【问题标题】:getting sender object in cocos2d在 cocos2d 中获取发送者对象
【发布时间】:2013-04-05 06:42:11
【问题描述】:

我正在使用 cocos2d 制作游戏。通过使用以下代码,我添加了一个动画。如何发送CCSprite 参考?

if(sprite != monkey)
{
    [self scheduleOnce:@selector(animate_sprite:) delay:0.1f];
}

-(void)animate_sprite:(ccTime) dt
{
    id s2 = [CCScaleTo actionWithDuration:0.5 scaleX:2.0 scaleY:2.0];
    id fun = [CCCallFuncN actionWithTarget:self selector:@selector(spriteDone:)];
    [sprite runAction:[CCSequence actions:s2,fun,nil]];
}

如何在animate_sprite方法中获取精灵引用?

【问题讨论】:

  • 让 sprite 成为 ivar,或者给它一个标签并使用 getchildbytag

标签: iphone objective-c xcode cocos2d-iphone ccsprite


【解决方案1】:

您可以使用performSelector:withObject:afterDelay 这会做同样的事情。

if(sprite != monkey)
{
    [self performSelector:@selector(animate_sprite:) withObject:sprite afterDelay:0.1f];
}

-(void)animate_sprite:(CCSprite *)sprite
{

    id s2 = [CCScaleTo actionWithDuration:0.5 scaleX:2.0 scaleY:2.0];
    id fun = [CCCallFuncN actionWithTarget:self selector:@selector(spriteDone:)];
    [sprite runAction:[CCSequence actions:s2,fun,nil]];
}

因此,只需编辑您的方法以使用 sprite 而不是 ccTime 对象,因为您根本没有使用它。

【讨论】:

  • 感谢您的回复。但是如何在 animate_sprite 方法中获取 sprite 对象?
  • 它在从 init 方法调用时工作。但是当我调用 from-(void) update: (ccTime) dt 这个方法动画不起作用。从 CCtime 动画不起作用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多