/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { BOOL moveX, moveY = NO; CGPoint touchLocation = [touch locationInView: [touch view]]; touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; touchLocation = [self convertToNodeSpace:touchLocation]; CGPoint playerPos = _player.position; CGPoint diff = ccpSub(touchLocation, playerPos); CGSize winSize = [[CCDirector sharedDirector] winSize]; // Define areas on the screen to control player differently // Centralised cross that will move directly up, down, left or right (winsize.width/3, winsize.height/3) // Remaining areas will move diagonally // If central crossover area is pressed, player will not move. // Left/right press if (touchLocation.x < winSize.width/3) { moveX = YES; } else if (touchLocation.x > ((winSize.width/3)*2)) { moveX = YES; } else { moveX = NO; } // Up/down press if (touchLocation.y < winSize.height/3) { moveY = YES; } else if (touchLocation.y > ((winSize.height/3)*2)) { moveY = YES; } else { moveY = NO; } if (moveX) { if (diff.x >= _tileMap.tileSize.width) { playerPos.x += _tileMap.tileSize.width; } else if (diff.x <= (_tileMap.tileSize.width * -1)) { playerPos.x -= _tileMap.tileSize.width; } } if (moveY) { if (diff.y >= _tileMap.tileSize.height) { playerPos.y += _tileMap.tileSize.height; } else if (diff.y <= (_tileMap.tileSize.height * -1)) { playerPos.y -= _tileMap.tileSize.height; } } if (moveX || moveY) { if (playerPos.x <= (_tileMap.mapSize.width * _tileMap.tileSize.width) && playerPos.y <= (_tileMap.mapSize.height * _tileMap.tileSize.height) && playerPos.y >= 0 && playerPos.x >= 0 ) { [self setPlayerPosition:playerPos]; } } }