我正在制作一个平台游戏。目前我只是想创造重力和平台。我有重力向下拍,但是当我的角色(玩家)与平台发生碰撞时,他会立即回到屏幕顶部并再次跌倒,并且每次玩家触摸平台时都会重复。这是我的代码:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class GameCode extends MovieClip {
var upKey:Boolean;
var leftKey:Boolean;
var rightKey:Boolean;
var jump:Boolean = false;
var xvelocity:int = 10;
var yvelocity:int = 0;
var gravity:Number = 7;
var startPosY:int;
var startPosX:int;
var lastPosY:int;
var lastPosX:int;
public function GameCode() {
// constructor code
}
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
}
function update(evt:Event){
moveCharacter();
yvelocity += gravity;
for (var i:int = 0; i < 10; i++){
if (!platform.hitTestObject(player)){
player.y = yvelocity;
}
if (platform.hitTestObject(player)){
yvelocity = 0;
player.y--;
}
}
}
function moveCharacter(){
lastPosY = player.y;
lastPosX = player.x;
if (leftKey == true){
player.x -= xvelocity;
}
if (rightKey == true){
player.x += xvelocity;
}
if (upKey == true){
if (jump == true){
player.y
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.UP){
upKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.UP){
upKey = false;
}
}
}
}
我的角色的实例名称是玩家,我的平台实例只是平台。显然,我想就如何改变这一点提供帮助;我想要的是让角色在重力下落下,降落在平台上,能够四处移动,当角色从重力下落到底部时。