gpt4 book ai didi

iphone - 连续触摸 ccMenuItem

转载 作者:行者123 更新时间:2023-11-28 20:28:42 25 4
gpt4 key购买 nike

我正在使用 cocos2d 创建一个程序,其中有 2 个 ccMenuItems,每个链接到一个图像:在这种情况下,一个是左箭头,另一个是右箭头。我的 View 中心还有一个图像,它会根据按下的箭头旋转。

当我将手指放在两个菜单项之一(左侧或右侧)上时,我希望只要我的手指在按钮上,中心图像就会旋转。那就是我迷路的地方。我尝试使用以下代码:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint p = [touch locationInView:[touch view]];

if (CGRectContainsPoint(leftArrow, p) || CGRectContainsPoint(rightArrow, p)) {
return YES;
}
return NO;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint p = [touch locationInView:[touch view]];

if (CGRectContainsPoint(leftArrow, p)) {
[gun runAction:[CCRepeatForever actionWithAction:[CCRotateTo actionWithDuration:0.2 angle:180]]];
}
if (CGRectContainsPoint(rightArrow, p)) {
[gun runAction:[CCRepeatForever actionWithAction:[CCRotateTo actionWithDuration:0.2 angle:0]]];

}

}

使用这段代码,当我按下两个菜单项之一时,甚至都没有调用 ccTouchBegan 方法。仅当我触摸其他地方时才调用该方法。

如何在按住 ccMenuItem 时处理连续 Action 。

感谢您的帮助!

最佳答案

您必须继承 CCMenuItem 才能获得此类行为:

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

@interface RepeatMenuItem : CCMenuItemSprite
{
CGFloat speed;
}

@end

和实现:

////////////////////////////////////////////////////////////////////
// RepeatMenuItem
////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark RepeatMenuItem

@implementation RepeatMenuItem


-(void) selected
{
[super selected];
block_(self);
speed = 0.8;
[self schedule:@selector(repeatEvent:) interval:speed];
}


-(void) unselected
{
[self unschedule:@selector(repeatEvent:)];
[super unselected];
}


-(void) activate
{
}


-(void) repeatEvent:(id)sender
{
CGFloat minSpeed = 0.05;

if (speed > minSpeed)
speed = speed/3;
if (speed < minSpeed)
speed = minSpeed;

[self unschedule:@selector(repeatEvent:)];
block_(self);
[self schedule:@selector(repeatEvent:) interval:speed];
}

@end

关于iphone - 连续触摸 ccMenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103501/

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