1

我正在编写的播放器控制器的算法遇到了一些问题。我正在使用 Cocos2D 制作平台风格的游戏(想想超级马里奥兄弟)。我的控制器有一个左右按钮,你触摸屏幕的右半边就可以跳跃。

我遇到的问题是当您按住右或左按钮时,然后按住屏幕跳跃并松开方向按钮,玩家将继续朝该方向前进,直到用户将手指从屏幕上抬起.

另一个问题是,如果用户按住屏幕跳跃,点击方向按钮,然后在仍然按住屏幕的同时放开方向,玩家将继续朝那个方向前进。播放器在释放屏幕时停止的一半时间,另一半播放器将继续释放,直到再次点击屏幕。

这是我的代码来注册控制器的哪一部分被窃听:

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch* touch in touches)
    {
        CGPoint point = [self convertTouchToNodeSpace: touch];

        // Create a rect around right half of the window
        CGSize winSize = [CCDirector sharedDirector].winSize;
        CGRect jumpBox = CGRectMake(winSize.width / 2,
                                    0,
                                    winSize.width / 2,
                                    winSize.height);


        // left button
        if (CGRectContainsPoint(leftButtonSprite.boundingBox, point))
        {
            self.isPressingLeftButton = YES;
            self.isPressingRightButton = NO;
        }

        // right button
        else if (CGRectContainsPoint(rightButtonSprite.boundingBox, point))
        {
            self.isPressingRightButton = YES;
            self.isPressingLeftButton = NO;
        }

        // player's trying to jump
        if (CGRectContainsPoint(jumpBox, point))
        {
            self.isTouchingScreen = YES;
        }
    }
}

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch* touch in touches)
    {
        CGPoint point = [self convertTouchToNodeSpace: touch];

        // Create a rect around right half of the window
        CGSize winSize = [CCDirector sharedDirector].winSize;
        CGRect jumpBox = CGRectMake(winSize.width / 2,
                                    0,
                                    winSize.width / 2,
                                    winSize.height);


        // left button
        if (CGRectContainsPoint(leftButtonSprite.boundingBox, point))
        {
//            CCLOG(@"Pressing left");
            self.isPressingLeftButton = YES;
            self.isPressingRightButton = NO;
        }

        // right button
        else if (CGRectContainsPoint(rightButtonSprite.boundingBox, point))
        {
//            CCLOG(@"Pressing right");
            self.isPressingRightButton = YES;
            self.isPressingLeftButton = NO;
        }

        // player's trying to jump
        if (CGRectContainsPoint(jumpBox, point))
        {
//            CCLOG(@"Jumping");
            self.isTouchingScreen = YES;
        }

    }
}

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isTouchingScreen)
    {
        CCLOG(@"Stop jumping");
        self.isTouchingScreen = NO;
    }
    else
    {
        CCLOG(@"Stop moving");
        self.isPressingLeftButton = NO;
        self.isPressingRightButton = NO;
    }
}

这段代码在我的HUDLayer.m里面,这是我的GameplayLayer.m. GameplayLayer 用于update:确定设置了 HUDLayer 中的哪些标志并相应地移动玩家

4

1 回答 1

0

弄清楚了。一种n00b错误。在ccTouchesEnded:withEvent:中,我必须convertTouchToNodeSpace检查它是否与方向按钮的矩形相交或跳过屏幕的一半,如ccTouchesBegan:withEvent:

希望有一天这对某人有所帮助。快乐编码:)

于 2013-08-08T18:37:21.923 回答