Lagoo87 Lagoo87 - 1 year ago 121
iOS Question

cocos2d v3 CCActionCallFunc with object?

In cocos2d previously I had used CCCallFuncN and CCCallFuncND

I am attempting to call a function and send the object through to the function. I've tried a couple implementations which don't seem to be working. My latest attempt with CCActionCallBlock looks like this:

[gun runAction:[CCActionRepeatForever actionWithAction:[CCActionCallBlock actionWithBlock:^
{
CGPoint gunPoint = CGPointMake(gun.position.x - gun.contentSize.width/2, gun.position.y);

CGPoint shootVector = ccpSub(gunPoint, _playerPos);
float gunAngle = -1 * ccpToAngle(shootVector);
gun.rotation = CC_RADIANS_TO_DEGREES(gunAngle);
}]]];


And I am receiving the following error:


-[CCActionCallBlock elapsed]: unrecognized selector sent to instance 0x15f84c50
2014-03-02 16:54:19.768 JuhnerickShooterFinal[22576:70b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCActionCallBlock elapsed]: unrecognized selector sent to instance 0x15f84c50'


Can someone shed some light on how to pass the object into the Called function from the Action.

Thanks :)

Answer Source

As you can see from the CCActionCallBlock reference, it's an instant action which doesn't contain the elapsed method, which is within the CCActionInterval class (and all subclasses).

If you want something to happen every frame then simply add the code to the node's update method:

Gun.m:

- (void)update:(ccTime)delay {
    GPoint gunPoint = CGPointMake(self.position.x - self.contentSize.width/2, self.position.y);
    CGPoint shootVector = ccpSub(gunPoint, _playerPos);
    float gunAngle = -1 * ccpToAngle(shootVector);
    self.rotation = CC_RADIANS_TO_DEGREES(gunAngle);
}

Note: you will need to figure out how to access _playerPos in the code above. Player could be accessed via a singleton-like pattern from the game scene.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download