Actionscript 3 检测是否";“玩家”;在AS3中跳到一个盒子上
我正在做一个简单的AS3游戏,玩家应该能够跳到一个盒子上。 如何检测玩家是否落在箱子上而没有撞到箱子 AS3让玩家跳跃:Actionscript 3 检测是否";“玩家”;在AS3中跳到一个盒子上,actionscript-3,flash,collision-detection,game-physics,Actionscript 3,Flash,Collision Detection,Game Physics,我正在做一个简单的AS3游戏,玩家应该能够跳到一个盒子上。 如何检测玩家是否落在箱子上而没有撞到箱子 AS3让玩家跳跃: var grav:Number = 10; var jumping:Boolean = false; var jumpPow:Number = 0; stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown); stage.addEventListener(Event.ENTER_FRAME, update); funct
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onDown(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.UP)
{
//umbau das mehrfach tab bis höhe erreicht?
if(jumping != true)
{
jumpPow = -10;
jumping = true;
}
}
}
function update(evt:Event):void
{
if(jumping)
{
player_mc.y += jumpPow;
jumpPow += grav;
if(player_mc.y >= stage.stageHeight)
{
jumping = false;
player_mc.y = stage.stageHeight;
}
}
}
游戏布局是这样的:(灰色框从右向左移动,玩家的位置是固定的)
您应该从一个简单的矩形碰撞函数开始:
/* Test collision between a rectangle and another rectangle. */
/* right() and bottom() return x+width and y+height of the parent rectangle. */
Rectangle.prototype.collideRectangle=function(rectangle_){
if (this.x>rectangle_.right()||this.y>rectangle_.bottom()||this.right()<rectangle_.x||this.bottom()<rectangle_.y){
return false;
}
return true;
}
/*测试一个矩形和另一个矩形之间的冲突*/
/*right()和bottom()返回父矩形的x+宽度和y+高度*/
Rectangle.prototype.collideRectangle=函数(矩形){
如果(this.x>rectangle_124;|||this.y>rectangle_124;.bottom()| this.right())
/* If true, the two rectangles are definitely colliding. */
if (red_rect.collideRectangle(gray_rect)){
/* Get the last position of the bottom of the red rectangle. */
var old_bottom=red_rect.bottom()-red_rect.velocity.y;
/* If the old bottom was above the gray rectangle on the last frame and is colliding in this frame, you know that the red rectangle is landing on the gray rectangle. */
if (old_bottom<gray_rect.y){
/* Place the red rectangle on top of the gray rectangle. */
red_rect.y=gray_rect.y-red_rect.height;
}
}