zoukankan      html  css  js  c++  java
  • [ActionScript 3.0] AS3.0 对象在矩形范围随机运动

    package com.views
    {
        
        import flash.display.Bitmap;
        import flash.display.MovieClip;
        import flash.display.Sprite;
        import flash.events.Event;
        
        /**
         * @author:Frost.Yen
         * @E-mail:871979853@qq.com
         * @create:    2016-7-7 下午2:33:56
         *
         */
        public class LogoView extends Sprite
        {
            private var _stageW:Number = 1920;
            private var _stageH:Number = 1080;
            private var _mc:MovieClip = new MovieClip();
            public function LogoView(bmp:Bitmap)
            {
                _mc.addChild(bmp);
                bmp.x = -bmp.width*0.5;
                bmp.y = -bmp.height*0.5;
                initLogo();
            }
        
            private function initLogo():void{
    
                //随机位置
                _mc.x = Math.random()*_stageW;
                _mc.y = Math.random()*_stageH;
                //随机速度和方向
                Math.random()>0.5?_mc.speedX = Math.random()*5+1 : _mc.speedX = -1 * Math.random()*5+1
                Math.random()>0.5?_mc.speedY = Math.random()*5+1 : _mc.speedY = -1 * Math.random()*5+1
                //因为logo的坐标原点是logo的中心,所以用该函数用来调整logo位置,防止移出边界
                adjustBall(_mc);
                //添加logo到显示列表
                addChild(_mc);
                //用ENTER_FRAME侦听器使logo运动
                //startMove();
            }
            
            private function adjustBall(mc:MovieClip):void{
                if(mc.x > _stageW-mc.width/2){mc.x = _stageW-mc.width/2;}
                if(mc.x < mc.width/2){mc.x = mc.width/2};
                if(mc.y > _stageH-mc.height/2){mc.y = _stageH-mc.height/2;}
                if(mc.y < mc.height/2){mc.y = mc.height/2;}
            }
            
            private function onEnterFrame(evt:Event):void{
                //改变logo的坐标让小球运动
                evt.target.x += evt.target.speedX;
                evt.target.y += evt.target.speedY;
                //检测logo是否碰到舞台边界
                if(evt.target.x >= _stageW-evt.target.width/2 || evt.target.x <= evt.target.width/2){
                    evt.target.speedX *= -1;
                }
                if(evt.target.y >= _stageH-evt.target.height/2 || evt.target.y <= evt.target.height/2){
                    evt.target.speedY *= -1;
                }
            }
            public function startMove():void
            {
                _mc.addEventListener(Event.ENTER_FRAME,onEnterFrame);
            }
            public function stopMove():void
            {
                _mc.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
            }
        }
    }
  • 相关阅读:
    Spring Cloud (八):服务调用追踪 sleuth & zipkin
    Spring Cloud (七):API 网关
    Spring Cloud (六):声明式 REST 请求 Feign
    maven 下载 jar 包到本地
    K8S 设置 Pod 使用 host 网络、配置 DNS
    Spring Cloud (五):容错处理 Hystrix
    Spring Cloud (四):客户端实现负载均衡
    [数仓]数据仓库设计方案
    [知识图谱]Neo4j知识图谱构建(neo4j-python-pandas-py2neo-v3)
    [Pandas]利用Pandas处理excel数据
  • 原文地址:https://www.cnblogs.com/frost-yen/p/5650856.html
Copyright © 2011-2022 走看看