Question

I have a node. In this particular case, it's a CCLayer, but I'm looking for a general solution. My node is centered at point1 (let's say { 100, 100 }). I'd like it to move to point1 (say { 200, 200 }) over the course of 0.5 seconds.

Really simple stuff, right? But I'm just not finding the docs/tutorials I need to do it.

Hints?

Thanks!

Extra credit: same question with a CC3Node, if the answer is different. :)

Was it helpful?

Solution

You can move anything inherits CCNode using runAction: [CCMoveTo actionWithDuration: 0.5 position:ccp(x,y)]

http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions

OTHER TIPS

// assuming you've already got a CCLayer called "myLayer"

[myLayer runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(200,200)]];

EDIT: Changed to CCMoveTo rather than CCMoveBy after re-reading the question.

/*

Moving an entire layer including all of his children (sprites, labels etc.). Insert this code into your 'init' method which belongs to the layer you'd like to be moved to the new point. X and Y are exactly coords of the new position of the layer relative to his center.

Example: x = 0; x = 100; in this case the layer will be moving vertical.

*/

x = ?; // X value

y = ?; // Y value

[self runAction:[CCMoveTo actionWithDuration:5.0f position:ccp(x, y)]];

Are you trying to move the layer or does your layer contain sprites that you want to move? i am not sure if it is even possible to move a layer (or stacks of layers) that contain childs (ccnodes, ccsprites, etc.).

My advice would be to move the layers child elements using ccanimation/ccmoveby/ccmoveto/etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top