zoukankan      html  css  js  c++  java
  • 碰撞检测(小球与舞台以及挡板碰撞的判断)

    package view
    {
    /**
     * @author zoe
     *
     */
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class GameScene extends Sprite
    {
        private var rect:MoveRect;
        private var ball:Ball;
        
        private var speedX:int;
        private var speedY:int;
        private var r:int;
        public function GameScene()
        {
            init();
        }
        
        private function init():void
        {
            rect = new MoveRect();
            ball = new Ball();
            addChild(rect);
            addChild(ball)
            
            rect.y = 560;
            ball.x = 300;
            ball.y = 300;
            
            speedX = 5;
            speedY = 5;
            r = ball.width/2;    
            
        }
        
        public function useEfHandler():void
        {
            addEventListener(Event.ENTER_FRAME,efHandler);
        }
        
        private function efHandler(event:Event):void
        {
            rect.x = mouseX - rect.width/2;        
            
            ball.x += speedX;
            ball.y += speedY;
            
            if(ball.x < r)
            {
                speedX = -speedX;
            }
            
            if(ball.x >= this.stage.stageWidth - r)
            {
                speedX = -speedX;
            }
            
            if(ball.y < r)
            {
                speedY = -speedY;
                trace(speedY);
            }
            
            if(ball.y >= this.stage.stageHeight - r)
            {
                speedY = -speedY;
            }
    //之所以全用if,没有else,是因为如果写成if,else if,else if,有一种情况符合走进去后就会完全跳出,当小球位置在四个角,例如同时符合ball.x < r和ball.y < r,就只会走进一种情况然后跳出方法,为了解决这个bug就全写成if。
    isHit(); 
    }
    private function isHit():void
    { if(ball.hitTestObject(rect))
    {
    // speedX = -speedX;因为碰到下面的挡板X坐标不需要取反的,只有y要变动
    speedY = -speedY;
    }
    }
    }
    }
  • 相关阅读:
    P1342 请柬
    P1186 玛丽卡
    Scala 中下划线的用法
    IDEA2017 maven Spark HelloWorld项目(本地断点调试)
    Spark内存管理详解
    Spark基础知识
    scala基本语法
    分布式锁的一点理解
    Redis并发问题
    redis集群原理
  • 原文地址:https://www.cnblogs.com/kuailezoe/p/2811969.html
Copyright © 2011-2022 走看看