【发布时间】:2016-04-12 11:19:32
【问题描述】:
在一些帮助下,我成功地创建了一个 2D 行星,它具有工作重力,不断将角色拉向其中心。现在唯一的问题是我完全不知道如何制作它,以便用户能够让这个角色像人们期望的那样穿越地球,而不是我们在自然平台游戏中习惯的简单的上/下/左/右。
需要注意的是,被穿越的行星会有平台/层可以跳跃和坠落,因此移动和跳跃应该与行星的中心相关,行星的中心作为“向下”会在传统的平台游戏中(这就是为什么我似乎认为我需要一种复杂的方式来创建这种自然运动)。
我的想法是在每次位置改变时重新计算一个新的左右方向以移动,但我必须制定出实现这一目标的逻辑超出了我的知识范围。
这是唯一的方法还是我应该尝试以不同的方式解决问题?我希望不是,因为我认为否则我无法实现这一目标。
这是我当前的代码,展示了重力是如何工作的,带有一个简单的移动占位符,只允许角色在屏幕上向上/向下/向左/向右移动:
public function moveChar(event:Event):void
{
if (leftPressed)
{
character.x -= mainSpeed;
}
if (rightPressed)
{
character.x += mainSpeed;
}
if (upPressed)
{
character.y -= mainSpeed;
}
if (downPressed)
{
character.y += mainSpeed;
}
}
public function gravity():void
{
//tan2(planet.y - player.y, planet.x - player.x)
angle = Math.atan2((planet1.y + 2245) - (character.y+5), (planet1.x+2245) - (character.x+5));
gravityX = Math.cos(angle) * gravitationalAcceleration;
gravityY = Math.sin(angle) * gravitationalAcceleration;
//distance = Math2.Pythagoras(planet1.x + 50, planet1.y + 50, character1.x + 5, character1.y + 5);
distance = Math.sqrt((yDistance * yDistance) + (xDistance * xDistance));
//Calculate the distance between the character and the planet in terms of x and y coordinate
xDistance = (character.x + 5) - (planet1.x + 2245);//320 to 50
yDistance = (character.y + 5) - (planet1.y + 2245);//320 to 50
//Make sure this distance is a positive value
if (xDistance < 0) {
xDistance = -xDistance;
}
if (yDistance < 0) {
yDistance = -yDistance;
}
//Update the current velocity before new velocity is calculated
initialXVelocity = finalXVelocity;
initialYVelocity = finalYVelocity;
//Send the character into the correct direction
character.x += gravityX;
character.y += gravityY;
//Basic collision detection of the surface, basic stop of gravity
if (distance <= planet1Radius) {
trace("hit!");
if (planet1.x - 2180 < character.x - 5) { //320 to 50
character.x -= gravityX*1.025;
}
else {
character.x += gravityX*1.025;
}
if (planet1.y - 2180 < character.y - 5) { //320 to 50
character.y -= gravityY*1.025;
}
else {
character.y += gravityY*1.025;
}
} else {
trace(" no hit!");
}
}
【问题讨论】:
标签: actionscript-3 game-physics flashdevelop gravity