Hi guys, i would move my sprite on the horizontal-axis and when it touch border return on the opposite direction...
can i do this?
TY
A fast, easy to use, free, and community supported 2D game engine
Hi guys, i would move my sprite on the horizontal-axis and when it touch border return on the opposite direction...
can i do this?
TY
CCMoveTo or CCMoveBy would do this nicely. You'll probably also want to use CCSequence to schedule one action to occur after the last on.
I did with CCsequence but don't work
-(void)moveplat:(ccTime)dt{
CGSize winSize = [CCDirector sharedDirector].winSize;
CCMoveTo *moveright = [CCMoveTo actionWithDuration:0.2 position:ccp(winSize.width , _plat8.position.y)];
CCMoveTo *moveleft = [CCMoveTo actionWithDuration:0.2 position:ccp(0 , _plat8.position.y)];
[_plat8 runAction:[CCSequence actions: moveleft, moveright, nil]];
}
_plat8 move to position 0 and stop it...
You need to include a repeat if you wish it to go left, right, left, right, left, right...and so on.
yes, but the problem is that didn't call moveright... sprite move to position (o,etc..) and stop it
Judging by the naming of your moving method you are calling the action more than once, possibly every frame, which means your action is getting overridden, you only need to call an action once.
If i put my code on function init it works, in another function no... O.o
As what varedis mentioned, this is happening because your function is called by a scheduler somewhere else in your code like so:
[self schedule:@selector(moveplat:) interval:1.0f];
It may be with or without the interval. However, what's important is that because of that the actions that are running on _plat8 is being overridden over and over again ~
Therefore the moveRight action will not be called because the moveLeft action is called repeatedly every 1 second (if your code is like the example on top).
What you need to do is to change your function name to:
-(void)moveplat {
//the code here
}
and call it somewhere else in your code only once
If you want it to move from the left border to the right border and back again, you can use CCRepeatForever in the function like so:
CCMoveTo *moveright = [CCMoveTo actionWithDuration:0.2 position:ccp(winSize.width , _plat8.position.y)];
CCMoveTo *moveleft = [CCMoveTo actionWithDuration:0.2 position:ccp(0 , _plat8.position.y)];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:[CCSequence actions:moveleft,moveRight, nil]];
[_plat8 runAction:repeat];You must log in to post.