gpt4 book ai didi

ios - 如何在 Cocos2D 中使 Sprite 在更新循环内跳转

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:08 25 4
gpt4 key购买 nike

我有一个球 Sprite 从顶部掉落,下面有一个男孩。当球击中男孩的头时,我希望它遵循抛物线路径。我尝试如下使用 CCJumpTo,但无法正常工作。我正在调用更新循环内的操作。我不允许这样做吗?我不能在更新循环中调用 CCJumpTo 吗?

- (void) jump
{
if(!method_called)
{
method_called=TRUE;
CCActionInterval *jump1 = [CCJumpTo actionWithDuration:3 position:CGPointMake(400, 400) height:150 jumps:1];
[_ball runAction:jump1];
NSLog(@"something");
}
else
{
NSLog(@"do nothing");
}
}

- (void)update:(ccTime)dt
{
time ++;

if ( dpad.leftJoystick.velocity.x > 0 && x < 2000 ) {
x = x + 10;
} else if ( dpad.leftJoystick.velocity.x < 0 && x > 0 ) {
x = x - 10;
}
if (x > 10 && x < 2000)
_boy.position = ccp(x, 100);


_ball.position = ccp(ball_x, ball_y);
ball_y = _ball.position.y - (speed * rebound);

_boy.anchorPoint = ccp(0, 0);
CGRect boyBox = CGRectMake(_boy.position.x, _boy.position.y, [_boy boundingBox].size.width, [_boy boundingBox].size.height);
_ball.anchorPoint = ccp(0, 0);
CGRect ballBox = CGRectMake(_ball.position.x, _ball.position.y, [_ball boundingBox].size.width, [_ball boundingBox].size.height);



if (CGRectIntersectsRect(boyBox, ballBox) && flag == 0)
{
rebound = -rebound;

flag = 1;
topFlag = 0;
[_ball stopAllActions];
[self jump];

}
if (_ball.position.y > 700 && topFlag == 0)
{
rebound = -rebound;
flag = 0;
topFlag = 1;
}
}

提前致谢。

编辑:

这是我的初始化方法

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.ball = [CCSprite spriteWithFile:@"ball.png"
rect:CGRectMake(0, 0, 50, 50)];
self.ball.position = ccp(275, 999);
[self addChild:self.ball];


x = 0;
ball_y = 650;
ball_x = 300;
rebound = 1;
flag = 0;
topFlag = 0;
speed = 5;
time = 0;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
@"boy.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"boy.png"];
//spriteSheet.scaleX = 0.5;
//spriteSheet.scaleY = 0.5;
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 8; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"boy%d.png", i]]];

CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:.1f];
self.boy = [CCSprite spriteWithSpriteFrameName:@"boy1.png"];
_boy.position = ccp(100000, 100000);
self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim]];
[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
[_boy runAction:_walkAction];
_boy.flipX = YES;
[spriteSheet addChild:_boy];

dpad = [[MyJoystick alloc] init];
[self addChild:dpad z:10];
[self schedule:@selector(update:)];
}

[self addChild:spriteSheet];

}
return self;
}

最佳答案

将您的操作代码保存在一个方法中,并使用 bool 变量从更新方法中调用它,这样它只会被调用一次。像这样:

在 .h 文件中取一个 bool 变量 method_called:

-(void)update:(ccTime)dt
{
_boy.anchorPoint = ccp(0, 0);
CGRect boyBox = CGRectMake(_boy.position.x, _boy.position.y, [_boy boundingBox].size.width, [_boy boundingBox].size.height);
_ball.anchorPoint = ccp(0, 0);
CGRect ballBox = CGRectMake(_ball.position.x, _ball.position.y, [_ball boundingBox].size.width, [_ball boundingBox].size.height);

if (CGRectIntersectsRect(boyBox, ballBox) && flag == 0)
{
flag = 1; topFlag = 0;
[self callJumpAction];
}
}

-(void)callJumpAction
{
if(!method_called)
{
method_called=true;
CCActionInterval *jump1 = [CCJumpTo actionWithDuration:3 position:CGPointMake(400, 400) height:150 jumps:1];
[_ball runAction:jump1];
}
else
{
NSLog(@"do nothing");
}
}

希望这会有所帮助。

关于ios - 如何在 Cocos2D 中使 Sprite 在更新循环内跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12275707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com