我尝试在 ActionScript 3 中创建一个平台游戏。我已经有一些用于移动和动画角色的代码,以及简单的平台样式,但我想让角色撞墙或将他的头撞到底部平台。这时候我有这个:
import flash.events.KeyboardEvent;
import flash.events.Event;
var KeyThatIsPressed:uint;
var rightKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var gravity:Number = 1;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var playerSpeed:Number = 7;
var crouchSpeed:Number = -4;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
function PressAKey(event:KeyboardEvent):void{
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = true;
}
if(event.keyCode == 38 || event.keyCode == 87){
upKeyDown = true;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
}
}
function ReleaseAKey(event:KeyboardEvent):void{
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upKeyDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = false;
}
}
char_mc.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
if(rightKeyDown){
char_mc.x += playerSpeed;
char_mc.scaleX = 1;
}
if(rightKeyDown && !downKeyDown && canJump == true){
char_mc.gotoAndStop(2);
}
if(leftKeyDown){
char_mc.x -=playerSpeed;
char_mc.scaleX = -1;
}
if(leftKeyDown && !downKeyDown && canJump == true){
char_mc.gotoAndStop(2);
}
if(upKeyDown && canJump){
yVelocity = -10;
canJump = false;
char_mc.gotoAndStop(3);
}
if(downKeyDown && !rightKeyDown && !leftKeyDown){
char_mc.gotoAndStop(4);
}
if(downKeyDown && rightKeyDown && canJump == true){
char_mc.x +=crouchSpeed;
char_mc.gotoAndStop(5);
}
if(downKeyDown && leftKeyDown && canJump == true){
char_mc.x -=crouchSpeed;
char_mc.gotoAndStop(5);
}
if(upKeyDown && canJump && rightKeyDown){
canJump = false;
char_mc.gotoAndStop(3);
}
if(upKeyDown && canJump && leftKeyDown){
canJump = false;
char_mc.gotoAndStop(3);
}
if(!rightKeyDown && !leftKeyDown && !upKeyDown && !downKeyDown){
char_mc.gotoAndStop(1);
}
if(yVelocity >0){
canJump = false;
}
if (canJump == false){
char_mc.gotoAndStop(3);
}
yVelocity +=gravity;
if(! floor_mc.hitTestPoint(char_mc.x, char_mc.y,true)){
char_mc.y+=yVelocity;
}
if(yVelocity > 20){
yVelocity = 20;
}
for(var i:int=0;i<10;i++){
if(floor_mc.hitTestPoint(char_mc.x,char_mc.y,true)){
char_mc.y--;
yVelocity=0;
canJump=true;
}
}
if (char_mc.hitTestObject(fallBox)){
char_mc.x = 20;
char_mc.y = 300;
}
}
在我用了 4 天在 youtube 或 google 或任何地方找到任何东西后,我来到了这里。我希望你能帮忙。谢谢!