我尝试在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或谷歌或任何地方找到任何东西后,我来到这里。我希望你能帮忙。谢谢!
答案 0 :(得分:1)
我建议使用像http://citrusengine.com/或其他引擎或http://www.box2dflash.org/这样的东西,这样可以让你轻松生活,而不是试图重新发明轮子。