我正在用flash ActionScript3制作一个游戏,已经基本完成了,但我有一些问题。
想想看,在这个游戏中,你是一个角色(目前是一个黑色的方块),你必须通过跳到正在下降的积木上来获得尽可能高的高度。现在我能做的就是跳跃,与方块(左-右)发生碰撞,然后跳到方块上。
当两个区块完全相邻时(区块A紧邻区块B),然后我跳上区块A,然后我想跑到区块B(只是跑而不跳),我得到了一些非常奇怪的东西,我不能在区块上行走。
以下是问题所在的代码:
package Classes{导入flash.events.*;导入flash.display.MovieClip;导入flash.display.Stage;导入flash.ui.Keyboard;导入Classes.KeyObject;
public class Player extends MovieClip
{
var isJumping:Boolean = false;
var jumpPower:int = 0;
var ground:int;
var stageRef:Stage;
var key:KeyObject;
var isDead:Boolean = false;
var allBlocks:Array;
var blockX:int;
var blockY:int;
var blockWidth:int;
var blockHeight:int;
var steps:int = 10;
var upPressed:Boolean = false;
public function Player(stageReff:Stage,allBlocks:Array)
{
stageRef = stageReff;
key = new KeyObject(stageRef);
this.allBlocks = allBlocks;
ground = stageRef.stageHeight;
addEventListener(Event.ENTER_FRAME,update);
//addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
function isHit()
{
for (var j = 0; j < allBlocks.length; j++)
{
if (this.hitTestObject(allBlocks[j]) && allBlocks[j] != isOnTopOf())
{
blockX = allBlocks[j].x;
blockY = allBlocks[j].y;
blockWidth = allBlocks[j].width;
blockHeight = allBlocks[j].height;
return true;
}
}
return false;
}
function isHitTop()
{
for (var j = 0; j < allBlocks.length; j++)
{
if (this.PlayerBottom.hitTestObject(allBlocks[j].hitBoxTop))
{
jumpPower = 0;
isJumping = false;
this.y = allBlocks[j].y - allBlocks[j].height;
return true;
}
}
return false;
}
function isHitBottom()
{
for (var j = 0; j<allBlocks.length; j++)
{
if (this.hitTestObject(allBlocks[j].hitBoxBottom))
{
if (isJumping)
{
this.y = allBlocks[j].y + this.height;
jumpPower=0;
isJumping = false;
isDead = false;
trace(isDead);
}
else
{
isDead = true;
trace(isDead);
}
}
}
}
function isOnTopOf()
{
for (var j = 0; j < allBlocks.length; j++)
{
if (this.PlayerBottom.hitTestObject(allBlocks[j].hitBoxTop))
{
trace("is WEL onTopOf");
return allBlocks[j];
}
}
trace("is NIET onTopOf");
return null;
}
function update(evt:Event):void
{
if (!isDead)
{
isHitTop();
isHitBottom();
if (key.isDown(Keyboard.UP))
{
//Voorkomen dat hij 2x springt
if (! isJumping)
{
jumpPower = 12;
isJumping = true;
}
}
if (key.isDown(Keyboard.LEFT))
{
if (this.x > stageRef.stageWidth - stageRef.stageWidth)
{
this.x -= steps;
if (isHit() == true)
{
this.x = blockX + blockWidth + 1;
//trace("is gehit maar staat er niet op");
}
}
}
else if (key.isDown(Keyboard.RIGHT))
{
if (this.x < stageRef.stageWidth - this.width)
{
this.x += steps;
if (isHit() == true)
{
this.x = blockX - this.width - 1;
this.y = blockY;
}
}
}
if (isJumping)
{
this.y -= jumpPower;
jumpPower -= 2;
isHitTop();
isHitBottom();
}
if (! isJumping && ! isHitTop())
{
this.y += 7;
}
if (this.y > ground)
{
this.y = ground;
isJumping = false;
}
}
else
{
removeEventListener(Event.ENTER_FRAME,update);
}
}
}
}发布于 2012-01-14 15:33:22
问题出在这里:
if (isHit() == true)
{
this.x = blockX - this.width - 1;
this.y = blockY;
}你在街区跳跃。然后您按向右箭头转到右侧(您将看到这行else if (key.isDown(Keyboard.RIGHT)))。在这段代码中,你可以检查是否有任何冲突。是的,因为你一直待在这条街上。所以你的角色会得到这个坐标:
this.x = blockX - this.width - 1;this.y = blockY;
但是blockX,blockY和this.width是固定的。所以你的角色是固定的。
发布于 2012-01-17 20:38:10
我已经解决了问题。它在函数isHit()中。我还添加了一些新功能,比如菜单、分数和twitter feed。
https://stackoverflow.com/questions/8856639
复制相似问题