0
藉助一點幫助,我已經創造出一個具有工作重力的2D行星,它不斷地將角色拉向中心。現在唯一的問題是我完全難以製作,所以用戶能夠讓這個角色像人們期望的那樣穿越這個星球,而不是我們在自然平臺上習慣的簡單的上/下/左/右。AS3/Flashdevelop - 控制一個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!");
}
}