• 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即可

  • 相关阅读:
    vue通过webpack打包后怎么运行
    vue中请求本地的json数据
    electron打包成桌面应用程序的详细介绍
    Web应用架构-Services
    Web应用架构-Full-text Search Service
    Web应用架构-Job Queue & Servers
    Web应用架构-Caching Service
    Web应用架构-Database
    设计模式:设计模式概述&JDK中的应用
    常见面试问题总结
  • 原文地址:https://www.cnblogs.com/flashbird/p/3345567.html
Copyright © 2020-2023  润新知