ActionScript学习笔记(六)——边界处理

通常边界处理成为一个游戏必须必备的因素之一,边界处理有很多种,虽然详细分类的话总结要很多,但是需要记住的也就是两个,一个边界迂回,还有一个就是边界反弹。

0、边界要素

     在做边界处理的时候,我们首先应该判断边界要素,也就是什么情况下是边界情况。因此我们需要几个变量来存储这些变量信息。

var left:int = 0;
var top:int = 0;
var right:int = stage.fullStageWidth; //这个属性只有手机项目存在
var bottom:int = stage.fullStageHeight; //这个属性只有手机项目存在

如果想让平台自动缩放,也应当配置下面的属性因素

stage.stageAlign = StageAlign.TOP_LEFT;
stage.stageMode = StageScaleMode.NO_SCALE;

1、边界迂回

if(sprite.x - sprite.width/2>right){
    sprite.x = left - ship.width / 2;
}else if(sprite.x + sprite.width/2 < left){
    sprite.x = right + sprite.width/2
}else if(sprite.y - sprite.height/2 > bottom){
    sprite.y = top - sprite.height / 2;
}else if(sprite.y + sprite.height/2 < top){
    sprite.y = bottom + sprite.height / 2;
}

2、边界反弹

我们采用的事角速度方式,来实现反弹效果的。bounce为边界碰撞速度的损失系数。

if(sprite.x - sprite.width/2>right){
    sprite.x = right - sprite.width / 2;
    vx * = bounce;
    angle = Math.PI - angle;
}else if(sprite.x + sprite.width/2 < left){
    sprite.x = sprite.width / 2;
    vx * = bounce;
    angle = Math.PI - angle;
}else if(sprite.y - sprite.height/2 > bottom){
    sprite.y = bottom - sprite.height / 2;
    vy * = bounce;
    angle * = -1;
}else if(sprite.y + sprite.height/2 < top){
    sprite.y = sprite.height / 2;
    vy * = bounce;
    angle * = -1;
}

另外一种加入不考虑角速度情况:

每一次进入相应的边界碰撞,对应方向上的速度乘以-1即可

原文地址:https://www.cnblogs.com/flashbird/p/3345567.html