0

我现在尝试在 cocos2d 中制作一个简单的游戏,我有类似的东西

#import "GameplayLayer.h"

@implementation GameplayLayer
-(id)init {
    self = [super init];
    if (self != nil) {
        CGSize screenSize = [CCDirector sharedDirector].winSize;
        // właczenie obsługi dotyku
        self.isTouchEnabled = YES;
        if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        }
        else{
            paddle1 = [CCSprite spriteWithFile:@"bijakiPhone.png"];
            paddle2 = [CCSprite spriteWithFile:@"bijakiPhone.png"];
            puck = [CCSprite spriteWithFile:@"krazekiPhone.png"];
        }
        //Polozenie i inicjalizacja paletki nr 1
        [paddle1 setPosition:
         CGPointMake(screenSize.width/2,
                     screenSize.height*0.17f)];
        [self addChild:paddle1];
        //Polozenie i inicjalizacja paletki nr 2
        [paddle2 setPosition:
         CGPointMake(screenSize.width/2,
                     screenSize.height*0.83f)];
        [self addChild:paddle2];
        //Polozenie i inicjalizacja krązka
        [puck setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
        [self addChild:puck];
    }
    return self;
}
//onEnter
- (void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}

//onExit
- (void)onExit
{
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super onExit];
}
-(BOOL)containsTouch:(UITouch *)touch {
    CGRect r=[paddle1 textureRect];
    CGPoint p=[paddle1 convertTouchToNodeSpace:touch];
    return CGRectContainsPoint(r, p );
}
-(BOOL)containsTouch2:(UITouch *)touch {
    CGRect r=[paddle2 textureRect];
    CGPoint p=[paddle2 convertTouchToNodeSpace:touch];
    return CGRectContainsPoint(r, p );
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    if ([self containsTouch:touch]){
       CCLOG(@"krarzek 1 tapniety");
        isTouched1 = YES;
    }


    if ([self containsTouch2:touch]){
        CCLOG(@"krarzek 2 tapniety");
        isTouched2 = YES;
    }

    return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    if (isTouched1){
        CGPoint newTouchLocation = [touch locationInView:touch.view];
        newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
        [paddle1 setPosition:newTouchLocation];

    }


    if (isTouched2){
        CGPoint newTouchLocation = [touch locationInView:touch.view];
        newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
        [paddle2 setPosition:newTouchLocation];
    }

}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    if (isTouched1){
        isTouched1=NO;
        CCLOG(@"krarzek 1 zwolniony");
}
    if (isTouched2){
        isTouched2=NO;
        CCLOG(@"krarzek 2 zwolniony");
    }
}
@end

工作 CCSprite 移动,但是当我同时触摸 2 个 CCSprite 时,它​​们会重叠!我怎样才能分别移动它们?对不起我的英语,谢谢你的帮助!

4

1 回答 1

0

您的问题的原因是您没有将触摸连接到您的桨的东西存储在某处。因此,如果两种触摸都在您的桨精灵内部,则isTouched1isTouched2变量都具有 value YES。因此,在您的ccTouchMoved:withEvent:方法中,在这种情况下,两个精灵都将被放置到相同的位置。将您的触摸存储在某个变量中,或者我建议为此使用字典,触摸作为键,您需要作为值移动的精灵。在这种情况下,您的ccTouchMoved:withEvent:方法将如下所示

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
        CGPoint newTouchLocation = [touch locationInView:touch.view];
        newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
        CCNode paddle = [_yourDict objectForKey: touch];
        [paddle setPosition:newTouchLocation];
}

确定精灵是否包含给定触摸的方法名称还不够好。如果不查看代码,我无法说出他们做了什么。

于 2013-03-25T00:46:09.953 回答