简体   繁体   中英

Can I run CCActionSequence more than one time?

I have scene like this:

MyScene : CCScene

MyScene has a property CCButton *myMenu

When I touch myMenu, myMenu runs a CCActionSequence (contains a CCActionCallFunc calling a selector).

It works fine (can call selector in CCActionCallFunc) for the first time I touch myMenu. But CCActionCallFunc doesn't call the selector for the subsequent requests.

How can i explain about that??? I used cocos2d v3

Example:

In 1st scene I have myMenu set target like this:

[self.myMenu setTarget:self selector:@selector(touchMyMenu:)];
- (void)touchMyMenu:(id)sender { NSArray *actions = [NSArray arrayWithObjects:[CCActionCallFunc actionWithTarget:self selector:@selector(callFunction1)], [CCActionCallFunc actionWithTarget:self selector:@selector(callFunction2)], nil]; CCActionSequence *sequence = [CCActionSequence actionWithArray:actions]; [self.myMenu runAction:sequence]; }
- (void)callFunction1 { // do something } - (void)callFunction2 { // I used CCDirector to push to 2nd scene }

It works fine the first time I touch myMenu. But after that I use CCDirector to pop to 1st scene. And now i touch again in myMenu but it neither calls callFunction1 nor callFunction2

Since you're calling the selectors in the same class ( self is the target), instead of doing this:

- (void)touchMyMenu:(id)sender {
NSArray *actions = [NSArray arrayWithObjects:
    [CCActionCallFunc actionWithTarget:self selector:@selector(callFunction1)],
    [CCActionCallFunc actionWithTarget:self selector:@selector(callFunction2)], nil];
CCActionSequence *sequence = [CCActionSequence actionWithArray:actions]; 
[self.myMenu runAction:sequence];
}

... you can simply call the selectors directly:

- (void)touchMyMenu:(id)sender {
[self callFunction1];
[self callFunction2];
}

The result will be the same.

If this is not what you want to do then your action setup was incorrect to begin with. For one, which node is running the above action sequence doesn't actually matter, it will still run the selectors in the MyScene class instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM