ios - 如何在 cocos2d v3 上启用多点触控?

标签 ios cocos2d-iphone controls game-physics

多年来,我一直在努力让它发挥作用。我搜索了所有网络,只能找到错误。

这是我在 HelloWorldScene.m 中初始化函数的代码

- (id)init
{
    // Apple recommend assigning self with supers return value
    self = [super init];
    if (!self) return(nil);

    // Enable touch handling on scene node

    [self setUserInteractionEnabled:YES];
    [self setMultipleTouchEnabled:YES];
    self.exclusiveTouch = NO;
    self.claimsUserInteraction = NO;
    // Create a colored background (Dark Grey)
    CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];
    [self addChild:background];

    //Physics rules -- (Must go under [self addChild:background])
    _physicsWorld = [CCPhysicsNode node];
    //Sets the gravity of the world.
    _physicsWorld.gravity = ccp(0,0);
    _physicsWorld.debugDraw = YES;
    _physicsWorld.collisionDelegate = self;
    [self addChild:_physicsWorld];
    //End Physics Rules

    // Add a sprite
    _player = [CCSprite spriteWithImageNamed:@"ship.png"];
    [_player setScaleX: .2f];
    [_player setScaleY: .2f];
    _player.position = ccp(110, self.contentSize.height / 2);
    _player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:1]; // 1
    _player.physicsBody.collisionGroup = @"playerGroup"; // 2
    _player.physicsBody.collisionType = @"playerRect";
    [_physicsWorld addChild:_player];

    playerDirection = @"none";




    // Create a back button
    CCButton *backButton = [CCButton buttonWithTitle:@"[ Menu ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    backButton.positionType = CCPositionTypeNormalized;
    backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
    [backButton setTarget:self selector:@selector(onBackClicked:)];
    [self addChild:backButton];

    // done
    return self;
}

这是我的实际多点触控代码

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

    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];

    CGPoint targetPosition = ccp(self.contentSize.width, _player.position.y);

    if(touchLocation.x <= 350 && touchLocation.y >= 150){

    playerDirection = @"up";

    }

    if(touchLocation.x <= 350 && touchLocation.y <= 150){

    playerDirection = @"down";

    }

    bulletsOnScreen++;


    if(touchLocation.x >= 351){

   CCSprite *projectile = [CCSprite spriteWithImageNamed:@"black.jpg"];
   [projectile setScaleX:.03f];
   [projectile setScaleY:.03f];
   projectile.position = _player.position;
   [self addChild:projectile ];

    CCActionMoveTo *actionMove   = [CCActionMoveTo actionWithDuration:1.5f position:targetPosition];
    CCActionRemove *actionRemove = [CCActionRemove action];
    [projectile runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
}

我只想能够同时移动和开火?任何帮助,将不胜感激。谢谢!

最佳答案

目前正在使用 touchBegan 但请尝试 -

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

代替

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event 

关于ios - 如何在 cocos2d v3 上启用多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24400165/

相关文章:

ios - UITableViewCell 自定义字体仅在表更新后显示

ios - 将应用程序发送到后台并从 XCTest 中的最近应用程序重新启动它

objective-c - MKStoreKit 中的 "No visible @interface for ' NSData ' declares the selector ' base64EncodedString"错误

ios - swift - 将 subview 添加到段控件会导致无效行为

android - 如何在 Exoplayer 中为下一个、上一个、倒回和前进添加监听器

ios - Expo ImagePicker 在 IOS Gallery 未打开时崩溃

ios - 在标签栏 Controller 中查看 Controller

iphone - 非 ARC 和弱属性 - ios 5

ios - Cocos-2d iOs-cclayer的不同内存释放行为

html - 视频和音频控件在html中不起作用