我正在为我正在编写的播放器控制器的算法遇到一些麻烦。我正在使用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中的哪些标志设置并相应地移动玩家
答案 0 :(得分:0)
想出来。有点n00b的错误。在ccTouchesEnded:withEvent:
中,我必须convertTouchToNodeSpace
并检查它是否与方向按钮的部分相交或跳过屏幕的一半,如ccTouchesBegan:withEvent:
希望有一天能帮到某人。快乐的编码:)