问题描述:

I've been trying for ages to get this to work. I've searched all of the web, and can find only errors.

Here is my code for my initialize function in my 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;

}

Here is my code for the actual multitouch

-(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]]];

}

I just want to be able to move and fire at the same time? Any help would be appreciated. Thank you!

网友答案:

currently using touchBegan but try -

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

instead of

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
网友答案:

After performing changes in:

AppController.mm.[eaglView setMultipleTouchEnabled:YES];

We can start coding for multitouch.

相关阅读:
Top