I have one sprite and I included TileMap in my code. However, how to move my sprite let say: move to (3,0) <-- tile location
and how do i retrieve the tile location which my sprite is standing on.
A fast, easy to use, free, and community supported 2D game engine
I have one sprite and I included TileMap in my code. However, how to move my sprite let say: move to (3,0) <-- tile location
and how do i retrieve the tile location which my sprite is standing on.
Position.x / tileWidth then round that down with floor().
i dont get it :(. ok here a bit more explanation:
I have sprite at tile (1,0) and when i click on one tile location let say (0,9). it will automatically snap at tile location (0,9) there. there must be a method call tileAt:(x,y). I dont know how to use this.
@sunsu, what is floor() ? sorry i dont get it.
Here what i want. the sprite only can move tile by tile. so it can go only UP/DOWN/LEFT/RIGHT not to the "North West", "South East" etc:
In the above picture,
How do i set the sprite move to the end location using tilemap location? not ccp. or cocos2d doesn't have the method to set the sprite to specific tile location. ? You have any idea?
=================To make my question simple way, here is the pseudocode ====================
[MoveSprite Attile(x,y)] -> how to use this method. does it exist in cocos2d?
[RetrieveSpriteLocation.x] - > return the x coordinate of tile where the sprite is standing on
[RetrieveSpriteLocation.y] - > return the y coordinate of tile where the sprite is standing on
hai Codemattic, great article. thanks. actually i'm new in cocos2d. anyway my simple question, how do i move my sprite to a tilemap location:
CODE:
mysprite.position = [tilemap tileAt(0,4)]; <==== this code is wrong but the concept i want is like this.
I know how to move a sprite by using the code MoveBy / MoveTo and setPosition: ccp(x,y);
but in tileMap, how do I move the sprite at exact location in tileMap (using tileMap coordinate)?
@sunsu wrote:
Position.x / tileWidth then round that down with floor().
===================================
thanks sunsu, for your formula and its really help me. I did retrieve my sprite location by using this code:
float tilex = floor(sprite.x / 32);
float tiley = floor(sprite.y / 32);
NSLog(@"The sprite is at tile: ( %f, %f )", tilex, tiley);
Now after got the location, now the problem is how do i move my sprite to the tile location when clicked on a screen? (same as above question)
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
if( touch )
{
CGPoint location = [touch locationInView: [touch view]];
CocosNode *human = [self getChildByTag:kTagHuman];
float tilex = floor(location.x / 32);
float tiley = floor(location.y / 32);
[human runAction: [MoveTo actionWithDuration:1 position: tileAt(tilex,tiley)]]; //<-------- something wrong here at position: tileAt(tilex,tiley) , I hope somebody can fix this.
}
// we ignore the event. Other receivers will receive this event.
return kEventIgnored;
}
x=tilex*tileWidth
y=tiley*tileHeight
or maybe if you want to go to the center of the tile
x=(tilex*tileWidth)+(0.5*tileWidth)
y=(tiley*tileHeight)+(0.5*tileHeight)
hey sorry for the late reply, your code is awesome. Thanks and now im able to move my object to the center location.
You must log in to post.