Objective c 操作完成后,如何运行方法
我想知道在Objective c 操作完成后,如何运行方法,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,我想知道在CCAction完成后如何调用一个方法(我还想在第一次调用后每隔2.5秒调用一次)。我有一个精灵移动到一个随机位置,我希望它在移动到随机位置后运行这个方法(一个射击子弹)。到目前为止,该方法仍在移动时被调用。有人能帮忙吗 以下是敌人的创建方法: (void)enemy1{ gjk= arc4random()%6; enemy1 = [CCSprite spriteWithFile:@"enemy1.png"]; int d = arc4random()%480
CCAction
完成后如何调用一个方法(我还想在第一次调用后每隔2.5秒调用一次)。我有一个精灵移动到一个随机位置,我希望它在移动到随机位置后运行这个方法(一个射击子弹)。到目前为止,该方法仍在移动时被调用。有人能帮忙吗
以下是敌人的创建方法:
(void)enemy1{
gjk= arc4random()%6;
enemy1 = [CCSprite spriteWithFile:@"enemy1.png"];
int d = arc4random()%480+480;
int o = arc4random()%320+320;
x = arc4random()%480;
if( x <= 480 && x>= 460){
x=x-100;
}
if(x <= 100){
x = x+50;
}
y = arc4random()%320;
if(y <=320 && y >= 290){
y = y-100;
}
if(y < 100){
y = y + 100;
}
enemy1.position = ccp(o,d);
[enemy1 runAction:[CCMoveTo actionWithDuration:3 position: ccp(x,y)]];
CCRotateBy *rotation = [CCRotateBy actionWithDuration:20 angle:1080];
CCRepeatForever * repeatforever = [CCRepeatForever actionWithAction:rotation];
[enemy1 runAction:repeatforever];
[self addChild:enemy1];
[enemy addObject :enemy1];
}
我知道我试图通过使y位置<320时进行拍摄来实现拍摄,但当它经过320位置时仍在移动。您可以制作一个动作序列,在序列的末尾可以给出一个回调函数,该函数将在最后执行。像这样的
[CCSequence actions:
[CCMoveTo actionWithDuration:3 position: ccp(x,y)],
[CCCallFunc actionWithTarget:self selector:@selector(methodToRunAfterAction)],
nil]];
您可以创建一个操作序列,在序列的末尾,您可以给出一个回调函数,该函数将在最后执行。像这样的
[CCSequence actions:
[CCMoveTo actionWithDuration:3 position: ccp(x,y)],
[CCCallFunc actionWithTarget:self selector:@selector(methodToRunAfterAction)],
nil]];
再加上Tayyab的回答,你只需在开始射击方法(或你想叫它的任何方法)中开始实际的射弹射击计划,该方法在你的移动动作结束时发射:
[CCSequence actions:
[CCMoveTo actionWithDuration:3 position: ccp(x,y)],
[CCCallFunc actionWithTarget:self selector:@selector(startShooting)],
nil]];
其中,startShooting的定义如下:
- (void) startShooting
{
// start scheduling your projectile to fire every 2.5 seconds
[self schedule:@selector(projectileShooting:) interval:2.5];
}
再加上Tayyab的回答,你只需在开始射击方法(或你想叫它的任何方法)中开始实际的射弹射击计划,该方法在你的移动动作结束时发射:
[CCSequence actions:
[CCMoveTo actionWithDuration:3 position: ccp(x,y)],
[CCCallFunc actionWithTarget:self selector:@selector(startShooting)],
nil]];
其中,startShooting的定义如下:
- (void) startShooting
{
// start scheduling your projectile to fire every 2.5 seconds
[self schedule:@selector(projectileShooting:) interval:2.5];
}
谢谢你!这几乎就是我想要的。缺少的是调用它的间隔。如何设置时间间隔?如果您想要时间间隔,请使用[CCDelayTime actionWithDuration:]将其按您希望的时间顺序进行设置,可能是在我想象的开始或结束时。我的意思是每隔2.5秒左右调用一次拍摄方法的时间间隔。请简单地输入您的计划代码:“[self schedule:@selector”(ProjectleShooting:)interval:2.5];“在“MethodTorUnteraction”中使用[CCSequence actions:[CCMoveTo actionWithDuration:3 position:ccp(x,y)],[CCCallFunc actionWithTarget:self schedule:@selector(ProjectleShooting:)interval:2.5],nil];它给我一个警告“class method”+actionWithTarget:scheduel:interval:“未找到”(返回类型默认为“id”)谢谢!这几乎是我想要的。缺少的是调用它的间隔。如何设置间隔?如果需要间隔,请使用[CCDelayTime actionWithDuration:]我的意思是每隔2.5秒左右调用一次shooting方法的间隔。简单地将您的计划代码:“[self schedule:@selector(projectleshooting:)interval:2.5];”放在“methodtorunteraction”中。我用[CCSequence actions:[CCMoveTo actionWithDuration:3位置:ccp(x,y)],[CCCallFunc actionWithTarget:self schedule:@selector(ProjectleShooting:)interval:2.5],nil];它给我一个警告“class method”+actionWithTarget:scheduel:interval:'找不到(返回类型默认为“id”)哇!谢谢,这正是我想要的!我可能要多花几天时间才能意识到如何做你所做的来解决我的问题哈哈。哇!谢谢,这正是我想要的!我可能要多花几天时间才能意识到如何做你所做的来解决我的问题哈哈。