How do i make a function that loops, and changes sprite's position?
so i need a function or something that can make my 2 sprites move up and down, all the time! from the app starts with out anything have been touched on the screen..
heres what i have tryed to do, but since it in my init, it only runs once.. how can i make this loop its self so its looks dynamic ?
if (bgSkyer == false) {
[bg3 runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(240,100)]];
[bg2 runAction:[CCMoveTo actionWithDuration:1.5 position:ccp(240,95)]];
bgSkyer = true;
}else {
[bg3 runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(240,112)]];
[bg2 runAction:[CCMoveTo actionWithDuration:1.5 posi开发者_C百科tion:ccp(240,80)]];
bgSkyer = false;
}
As shown in Jebego's answer, you need to use CCRepeatForever
. However, assuming you are using bgSkyer
as a flag to toggle between the two positions, we can use skip that flag altogether if you are using CCRepeatForever
together with CCSequence
, as shown below (explicit variables are used for clarity but you can always merge them into one-liners if you want):
CCMoveTo *moveTo_240_95 = [CCMoveTo actionWithDuration:1.5 position:ccp(240,95)];
CCMoveTo *moveTo_240_80 = [CCMoveTo actionWithDuration:1.5 position:ccp(240,80)];
CCSequence *actionsForBg2 = [CCSequence actions:moveTo_240_95, moveTo_240_80, nil];
CCAction *repeatForBg2 = [CCRepeatForever actionWithAction:actionsForBg2];
[bg2 runAction:repeatForBg2]
CCMoveTo *moveTo_240_100 = [CCMoveTo actionWithDuration:0.5 position:ccp(240,100)];
CCMoveTo *moveTo_240_112 = [CCMoveTo actionWithDuration:0.5 position:ccp(240,112)];
CCSequence *actionsForBg3 = [CCSequence actions:moveTo_240_100, moveTo_240_112, nil];
CCAction *repeatForBg3 = [CCRepeatForever actionWithAction:actionsForBg3];
[bg3 runAction:repeatForBg3]
CCSequence
performs the actions you pass to it one after another in a sequence, CCRepeatForever
will repeat the sequence forever, until you call [node stopAllActions]
on the node or until the node is dealloc'ed.
I think something like this may be what you're looking for:
if (bgSkyer == false) {
CCMoveTo *moveOne = [CCMoveTo actionWithDuration:0.5 position:ccp(240,100)];
CCRepeatForever *repeatOne = [CCRepeatForever actionWithAction:moveOne];
[bg3 runAction:repeatOne];
CCMoveTo *moveTwo = [CCMoveTo actionWithDuration:1.5 position:ccp(240,95)];
CCRepeatForever *repeatTwo = [CCRepeatForever actionWithAction:moveTwo];
[bg3 runAction:repeatTwo];
}else{
//just repeat code, etc...
}
Hope it works!
精彩评论