我正在使用cocos2d创建一个程序,其中有两个ccMenuItems分别链接到一个图像:在本例中,一个是左箭头,另一个是右箭头。我在视图的中心也有一个图像,它将根据按下的箭头进行旋转。
当我将手指放在两个菜单项中的一个上时,无论是左还是右,只要我的手指在按钮上,我就希望中心图像旋转。这就是我迷路的地方。我尝试使用以下代码:
- (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键的同时处理连续的动作。
谢谢你的帮助!
发布于 2012-10-28 04:34:14
据我所知,您发布的两个方法在您的CCLayer的子类中。
首先,如果你想自己处理触摸,你必须删除菜单项。CCMenu有更多的触摸优先级,如果它处理了触摸命中任何菜单项,它就会吞下它。这就是为什么只有当触摸在菜单项之外时,你才会收到触摸。
第二个问题,什么是leftArrow和rightArrow?你的箭是怎么回事?
发布于 2012-10-28 04:36:06
你必须继承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发布于 2012-10-28 04:34:49
您可以扩展用于这些菜单项的CCMenuItemWhatever类,并覆盖“selected”和“unselected”方法。菜单抓取触摸(例如,IT在自己的ccTouchBegan函数上回答是,因此触摸调度程序不会将其传播给您)。
-(void) selected{
[super selected];
// start your animation here
}
-(void) unselected {
[super unselected];
// stop your animation here.
}https://stackoverflow.com/questions/13103501
复制相似问题