Actionscript3 / Starling设置使图像跟随触摸(同时拖动)移动应用程序

时间:2014-05-26 02:05:00

标签: actionscript-3 flash mobile flash-builder starling-framework

我试图在触摸屏幕时显示图像。当我的手指在屏幕上时,我希望图像跟随我的手指。当我松开手指时,我希望图像停止移动并保持在我松开手指时的位置。这是我第一次使用触摸屏,所以我意识到我的一些代码可能已经过时了。这是来自Game.as的代码(它在名为Startup.as的文件中初始化,这是一个主文件):

Game.as

package
{

import com.demonsters.debugger.MonsterDebugger;

import Classes.Avatar;
import Classes.AvatarEnemy;

import starling.display.Sprite;
import starling.events.EnterFrameEvent;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;

    public class Game extends Sprite
    {   
        public var enemy:AvatarEnemy;
        public var avatar:Avatar;

        private var paused2:Boolean = false;

        private var counter:int = 0;

        [Embed(source='Classes/Avatarpic.png')]
        private static var Avatarpic:Class;

        public function Game() 
        {   
            avatar.addEventListener(TouchEvent.TOUCH, onTouch);

            addEventListener(EnterFrameEvent.ENTER_FRAME, doCounter);

            enemy = new AvatarEnemy();
            addChild(enemy);

            avatar = new Avatar();
        }

        private function onTouch(e:TouchEvent):void
        {
            addChild(avatar);

            if (paused2) {
                return;
            }

            var touch:Touch = e.getTouch(avatar, TouchPhase.BEGAN);
            var touch2:Touch = e.getTouch(avatar, TouchPhase.HOVER);
            var touch3:Touch = e.getTouch(avatar, TouchPhase.ENDED);
            if (touch.phase == TouchPhase.BEGAN && !paused2) {
                avatar.addEventListener(EnterFrameEvent.ENTER_FRAME, withImage);
            }
            if (touch2.phase == TouchPhase.HOVER) {
                stage.color = 0xff0000;
                removeEventListener(EnterFrameEvent.ENTER_FRAME, doCounter);
                avatar.removeEventListener(TouchEvent.TOUCH, onTouch);
                paused2 = true;
            }
            if (touch3.phase == TouchPhase.ENDED) {
                avatar.removeEventListener(EnterFrameEvent.ENTER_FRAME, withImage);
            }

            function withImage():void {
                avatar.x = touch.globalX;
                avatar.y = touch.globalY;
            }
        }

        public function getScore():int {
            return counter;
        }

        public function getIsOver():Boolean {
            return paused2;
        }

        private function doCounter():void
        {
            counter++;
        }
    }
}

我首先为TouchEvent添加一个监听器 - 它调用方法onTouch,这是我尝试使应用程序执行上述操作。有一个计数器,最终成为游戏的“得分”(已经过了多少帧) - 并且paused2变量在游戏结束时处理。任何帮助将不胜感激。现在,当我在我的iPhone上测试时,我看到的只是一个白色的屏幕......当我触摸屏幕时,头像没有添加addChild(avatar)。此外,enemy精灵也没有出现,即使我使用addChild(enemy)添加它。谢谢!

更新

此代码替换上述代码:

似乎TouchEvent监听器根本没有触发(我使用trace来推断这一点) - 我根据处理的内容改变了一些东西,所以我需要粘贴更多代码:

Game.as

package
{

import com.demonsters.debugger.MonsterDebugger;

import Classes.Avatar;
import Classes.AvatarEnemy;

import starling.display.Sprite;
import starling.events.EnterFrameEvent;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import starling.events.Event;

    public class Game extends Sprite
    {   
        public var enemy:AvatarEnemy;
        public var avatar:Avatar;

        private var paused2:Boolean = false;

        [Embed(source='Classes/Avatarpic.png')]
        private static var Avatarpic:Class;

        public function Game() 
        {               
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
            addEventListener(TouchEvent.TOUCH, onTouch); //On touch of this image, do onTouch
        }

        private function onAdded():void {
            enemy = new AvatarEnemy();
            enemy.x = stage.stageWidth/2;
            enemy.y = stage.stageHeight/2;
            addChild(enemy);

            avatar = new Avatar();
            avatar.x = stage.stageWidth/3;
            avatar.y = stage.stageHeight/3;
            avatar.touchable = true;
            avatar.name = "avatar";
            addChild(avatar);
            trace(getChildAt(0));
        }

        private function onTouch(e:TouchEvent):void
        {
            trace("you touched it");

            if (paused2) {
                return;
            }

            var touch:Touch = e.getTouch(avatar, TouchPhase.BEGAN);
            var touch2:Touch = e.getTouch(avatar, TouchPhase.HOVER);
            var touch3:Touch = e.getTouch(avatar, TouchPhase.ENDED);
            if (touch.phase == TouchPhase.BEGAN && !paused2) {
                /*stage.color = 0xff0000;
                paused2 = true;*/
                avatar.addEventListener(EnterFrameEvent.ENTER_FRAME, withImage);
            }
            if (touch2.phase == TouchPhase.HOVER) {
                stage.color = 0xff0000;
                //removeEventListener(EnterFrameEvent.ENTER_FRAME, withImage);
                removeEventListener(TouchEvent.TOUCH, onTouch);
                paused2 = true;
            }
            if (touch3.phase == TouchPhase.ENDED) {
                avatar.removeEventListener(EnterFrameEvent.ENTER_FRAME, withImage);
            }
            /*if (touch3.phase == TouchPhase.ENDED && paused2) {
            removeEventListener(EnterFrameEvent.ENTER_FRAME, withImage);
            image2.removeEventListener(TouchEvent.TOUCH, onTouch);

            }*/

            function withImage():void {
                avatar.x = touch.globalX;
                avatar.y = touch.globalY;
            }
        }

        public function getIsOver():Boolean {
            return paused2;
        }
    }
}

Startup.as

package 
{

    import com.demonsters.debugger.MonsterDebugger;

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.system.Security;

    import Classes.Avatar;

    import io.socket.flash.ISocketIOTransport;
    import io.socket.flash.ISocketIOTransportFactory;
    import io.socket.flash.SocketIOErrorEvent;
    import io.socket.flash.SocketIOEvent;
    import io.socket.flash.SocketIOTransportFactory;
    import io.socket.flash.XhrPollingTransport;

    import starling.core.Starling;
    import starling.events.EnterFrameEvent;
    import starling.events.Touch;
    import starling.events.TouchEvent;
    import starling.events.TouchPhase;


    [SWF(width="640", height="960", frameRate="30", backgroundColor="#FFFFFF")]
    public class Startup extends Sprite
    {       
        private var mStarling:Starling;

        //Create referencable cursor image.
        //[Embed(source="Classes/Avatarpic.png")]
        //public const Cursor:Class;

        /*public var cursor;
        private var cursorBitmapData:BitmapData;
        private var cursorData:MouseCursorData;
        private var cursorVector:Vector.<BitmapData>;*/

        private var _socketIOTransportFactory:ISocketIOTransportFactory = new SocketIOTransportFactory();
        private var _ioSocket:ISocketIOTransport;
        //private var enemyaccess:AvatarEnemy;
        private var Game2:Game;

        private var counter:int = 0;

        public function Startup()
        {
            Security.loadPolicyFile("xmlsocket://127.0.0.1:10843");

            createSocket();
            MonsterDebugger.initialize(this);

            // Create a Starling instance that will run the "Game" class
            mStarling = new Starling(Game, stage);
            mStarling.start();

            //createCustomCursor(); //Create cursor

            // These settings are recommended to avoid problems with touch handling
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            mStarling.addEventListener(starling.events.Event.ROOT_CREATED, onAdded);
        }

        private function onTouch(e:TouchEvent):void {
            var touch:Touch = e.getTouch(Game2, TouchPhase.BEGAN);
            if(touch.phase == TouchPhase.BEGAN) {
                trace("touched");
            }
        }

        private function checkForScore(Event):void {
            if(Game2.getIsOver()) {
                MonsterDebugger.trace(this, "Made it to score check:" + " " + counter);
                send(int(counter));
                Game2.removeEventListener(EnterFrameEvent.ENTER_FRAME, checkForScore);
                Game2.removeEventListener(TouchEvent.TOUCH, onTouch);
            }
            counter++;
        }

        private function onAdded():void {
            MonsterDebugger.trace(this, "Root created");
            //enemyaccess = (Starling.current.root as Game).enemy;
            Game2 = (Starling.current.root as Game);
            Game2.addEventListener(TouchEvent.TOUCH, onTouch);
            Game2.addEventListener(EnterFrameEvent.ENTER_FRAME, checkForScore);
        }

        /*private function createCustomCursor():void
        {   
            cursor = new Cursor();
            cursorBitmapData = new BitmapData(32, 32, true, 0x000000); //Create empty bitmapdata object.
            cursorBitmapData.draw(cursor); //draw cursor on canvas of bitmapdata object.

            cursorVector = new Vector.<BitmapData>();
            cursorVector[0] = cursorBitmapData; //store image in vector.

            cursorData = new MouseCursorData();
            cursorData.hotSpot = new Point(0, 0);
            cursorData.data = cursorVector;

            //Register cursors
            Mouse.registerCursor("cursor", cursorData);
            Mouse.cursor = "cursor";

        }*/

        private function createSocket():void {
            _ioSocket = _socketIOTransportFactory.createSocketIOTransport(XhrPollingTransport.TRANSPORT_TYPE, "192.168.1.7:8000/socket.io", this);
            _ioSocket.addEventListener(SocketIOEvent.CONNECT, onSocketConnected);
            _ioSocket.addEventListener(SocketIOEvent.DISCONNECT, onSocketDisconnected);
            _ioSocket.addEventListener(SocketIOEvent.MESSAGE, onSocketMessage);
            _ioSocket.addEventListener(SocketIOErrorEvent.CONNECTION_FAULT, onSocketConnectionFault);
            _ioSocket.addEventListener(SocketIOErrorEvent.SECURITY_FAULT, onSocketSecurityFault);
            _ioSocket.connect();
        }

        private function onSocketConnectionFault(event:SocketIOErrorEvent):void
        {
            MonsterDebugger.trace(this, event.type + ":" + event.text);
        }
        private function onSocketSecurityFault(event:SocketIOErrorEvent):void
        {
            MonsterDebugger.trace(this, event.type + ":" + event.text);
        }
        private function onDisconnectClick():void
        {
            _ioSocket.disconnect();
        }
        private function onSocketMessage(event:SocketIOEvent):void
        {
            if (event.message is String)
            {
                MonsterDebugger.trace(this, String(event.message));
            }
            else
            {
                MonsterDebugger.trace(this, JSON.stringify(event.message));
            }
        }
        public function send(data:Object):void
        {
            _ioSocket.send(data);
        }
        private function onSocketConnected(event:SocketIOEvent):void
        {
            MonsterDebugger.trace(this, "Connected" + event.target);
        }
        private function onSocketDisconnected(event:SocketIOEvent):void
        {
            MonsterDebugger.trace(this, "Disconnected" + event.target);
        }
    }
}

Avatar.as和AvatarEnemy.as只需使用addChild(image)添加图片;

现在关注的重点是:

来自Startup.as:

Game2 = (Starling.current.root as Game);
Game2.removeEventListener(EnterFrameEvent.ENTER_FRAME, checkForScore);
Game2.removeEventListener(TouchEvent.TOUCH, onTouch);

onTouch方法:

private function onTouch(e:TouchEvent):void {
    var touch:Touch = e.getTouch(Game2, TouchPhase.BEGAN);
    if(touch.phase == TouchPhase.BEGAN) {
        trace("touched");
    }
}

当我触摸屏幕时,onTouch方法中的跟踪不会记录在控制台中。

0 个答案:

没有答案