瓷砖游戏对角线移动
我正在制作一款瓷砖游戏。正在关注 http://www. raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d。使用整个屏幕作为位于中间的角色的操纵杆。尽管看起来像这样,但移动仅在四个方向上:
如果中心是角色图块,请执行以下操作:如果触碰那里就不能动。如果触摸右侧等则向右移动...
我想让它在 8 个方向上工作。我尝试了一些东西,然后发现了这个: http://snipplr.com/view/39707/full-direction-movement-of-player-in-cocos2d-tilemap/
将原始代码扩展为处理8个方向。我修改了它并从 ccTouchMoved 和 ccTouchEnded 调用它。
- (void)movePlayer:(CGPoint)to {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGPoint playerPos = _player.position;
if (to.x < ((winSize.width/2)-((_tileMap.tileSize.width/2)*self.scale))) {
playerPos.x -= _tileMap.tileSize.width;
} else if (to.x > ((winSize.width/2)+((_tileMap.tileSize.width/2)*self.scale))) {
playerPos.x += _tileMap.tileSize.width;
}
if (to.y < ((winSize.height/2)-((_tileMap.tileSize.height/2)*self.scale))) {
playerPos.y += _tileMap.tileSize.height;
} else if (to.y > ((winSize.height/2)+((_tileMap.tileSize.height/2)*self.scale))) {
playerPos.y -= _tileMap.tileSize.height;
}
if (CGPointEqualToPoint(self.player.position, playerPos) == NO) {
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];
[self setViewpointCenter:self.player.position];
}
}
}
它看起来像这样:
这有一个问题;当你缩小时,通过在缩小的水平和垂直条内单击来走直线变得越来越困难......
第二个原作者将屏幕分成三部分以获得他的运动条。我想用角色图块来缩放我的,以便角色附近的运动保持准确。
所以我想我需要将该区域分成8等份才能使其发挥最佳效果?像这样的东西:
谁能解释如何检查这些边界内的触摸?或者我是否以正确的方式去做?
谢谢!
I am making a tile game. Was following http://www.raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d. Using the whole screen as the joystick for a character that stays in the middle. The movement is only in four directions though looking something like this:
Where the center is the character tile, do not move if touched there. Move right if touched right side etc...
I wanted to make it work with 8 directions. I tried a few things and then found this: http://snipplr.com/view/39707/full-directional-movement-of-player-in-cocos2d-tilemap/
Which expands the original code to handle 8 directions. I modified it and call it from ccTouchMoved and ccTouchEnded.
- (void)movePlayer:(CGPoint)to {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGPoint playerPos = _player.position;
if (to.x < ((winSize.width/2)-((_tileMap.tileSize.width/2)*self.scale))) {
playerPos.x -= _tileMap.tileSize.width;
} else if (to.x > ((winSize.width/2)+((_tileMap.tileSize.width/2)*self.scale))) {
playerPos.x += _tileMap.tileSize.width;
}
if (to.y < ((winSize.height/2)-((_tileMap.tileSize.height/2)*self.scale))) {
playerPos.y += _tileMap.tileSize.height;
} else if (to.y > ((winSize.height/2)+((_tileMap.tileSize.height/2)*self.scale))) {
playerPos.y -= _tileMap.tileSize.height;
}
if (CGPointEqualToPoint(self.player.position, playerPos) == NO) {
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];
[self setViewpointCenter:self.player.position];
}
}
}
and it looks something like this:
This has a problem; as you zoom out, it becomes harder and harder to walk in a straight line by clicking inside the shrinking horizontal and vertical bars...
The second original author divided the screen into thirds to get his movement bars. I wanted to scale mine with the character tile so movement remains accurate near the character.
So I think I need to divide the area into 8 equal parts to make it work best? Something like this:
Can anyone explain how to check for touches within these boundries? Or if I am going about it in the right way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于如何制作操纵杆对象的精彩教程可以在这里找到:71 平方如何制作游戏手柄。
至于上面提到的 8 个方向之一的运动,你需要做的就是(当然是在阅读并实现教程之后!) 算出你画的那 8 条线的角度,并检查新计算的角度是否落在在其中任何一对内。
如果您想要一些有关如何检查 8 个方向的示例代码,您可以这样做:
显然,这些数字在我的示例中是任意的。您只需要计算出上面绘制的 8 条线的 atan2(height, width) 值,这甚至可以通过 xcode 通过
NSLog(@"The angle is: %f “,atan2(高度,宽度));
。您还必须有 8 个案例(而不是 4 个),并且只需遵循上面的逻辑即可。希望有帮助!A great tutorial on how to make a joystick object can be found here: 71-squared How to make a Joypad.
As for the movement in one of the 8 directions mentioned above, all you need to do (after reading and implementing the tutorial of course!) is to figure out the angle of those 8 lines you drew, and check if the newly calculated angles fall within any pair of them.
If you want some sample code on how to check the 8 directions, you can do it like this:
Obviously, these numbers are arbitrary in my example. You need only figure out the value of
atan2(height, width)
for those 8 lines you drew above, which can be done even through xcode byNSLog(@"The angle is: %f", atan2(height, width));
. You'll also have to have 8 cases as opposed to four, and just follow the logic from above. Hope that helps!