德裕库 - 当我尝试改变光标移到图片...它不工作(从舞台精灵元素自败) [英] Starling library - When I try and change cursor to image...it doesn't work (and a sprite element dissapears from stage)

查看:234
本文介绍了德裕库 - 当我尝试改变光标移到图片...它不工作(从舞台精灵元素自败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用八哥做一个真的,真的,真的很简单的游戏 - 我只是想添加静止子画面的阶段......并使其使得当鼠标触摸精灵...游戏停止和得分被发送。我还没有尝试过实施则hitTest 还没有为碰撞,但我遇到了一种冲突的问题在哪里,当我注释掉线(S)是应该改变光标图像(见 Startup.as - stage.addEventListener(TouchEvent.TOUCH,touchHandler); createCustomeCursor ),AvatarEnemy(实例见敌人在Game.as)做的事情应该和被放置在中心屏幕。当我在该行评论说,应该改变光标:一)。光标并没有改变,和b)的敌人精灵消失。当我注释掉了同样的思路 - 对敌人精灵重新出现(但显然,光标不工作 - 不,这是摆在首位的工作)。这究竟是为什么?我的code低于 - 有人提到一些有关不做事八哥之前被初始化<一个href="http://stackoverflow.com/questions/23772083/starling-library-sprite-dissapeared-from-stage-and-i-dont-know-why?noredirect=1#comment36562854_23772083">here (一个问题,我问,这是precursor这一个) - 但我不知道他们的意思,因为它至少好像我所有的code是在正确的地方

I am using Starling to make a really, really, really easy game - I am just trying to add a stationary sprite to the stage...and make it so that when the mouse touches the sprite...the game "stops" and a score is sent. I haven't tried implementing hitTest yet for the collision, but I have run into a sort of conflict problem where, when I comment out the line(s) that is supposed to change the cursor image (see Startup.as - stage.addEventListener(TouchEvent.TOUCH, touchHandler); and createCustomeCursor), the instance of AvatarEnemy (see enemy in Game.as) does what it should, and is placed in the center of the screen. When I comment in the line that is supposed to change the cursor: a). the cursor doesn't change, and b.) the enemy sprite disappears. When I comment out the same lines - the enemy sprite reappears (but obviously, the cursor doesn't work - not that it was working in the first place). Why is this happening? My code is below - someone mentioned something about not doing things before Starling is initialized here (a question I asked, which is a precursor to this one) - but I'm not sure what they meant because it at least seems like all my code is in the right place.

Game.as

package
{

import Classes.AvatarEnemy;

import starling.display.Sprite;

    public class Game extends Sprite
    {
        //private var juggler:Juggler = Starling.juggler;

        private var enemy:AvatarEnemy;

        public function Game() 
        {   
            enemy = new AvatarEnemy();
            addChild(enemy);
        }
    }
}

Startup.as

package 
{
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.geom.Point;

    import starling.core.Starling;
    import starling.events.Touch;
    import starling.events.TouchEvent;
    import starling.display.DisplayObject;
    import flash.display.BitmapData;
    import flash.ui.MouseCursorData;
    import flash.ui.Mouse;

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

        [Embed(source="Classes/Avatarpic.png")]
        private const Cursor:Class;

        public var cursor:DisplayObject;

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

            // These settings are recommended to avoid problems with touch handling
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            createCustomCursor();
            stage.addEventListener(TouchEvent.TOUCH, touchHandler);
        }

        private function touchHandler(event:TouchEvent):void
        {
            var touch:Touch = event.getTouch(cursor);
            if(touch.phase){
                {
                    var localPos:Point = touch.getLocation(cursor);
                    trace("Touched object at position: " + localPos);
                }
            }
        }

        public function createCustomCursor():void
        {
            var cursorBitmaps:Vector.<BitmapData> = new Vector.<BitmapData>();
            cursorBitmaps.push((new Cursor() as Bitmap).bitmapData);

            var mouseCursorData:MouseCursorData = new MouseCursorData();
            mouseCursorData.data        = cursorBitmaps;
            mouseCursorData.frameRate   = 30;
            mouseCursorData.hotSpot     = new Point(0, 0);

            Mouse.registerCursor("customCursor", mouseCursorData);
            Mouse.cursor = "customCursor";
        }
    }
}

任何帮助将是很大的AP preciated(如果你需要的code(这是一个Adobe Flash Builder的4.7项目的副本 - 我做了一个混帐回购协议 - 只要你想要的链接)。

Any help would be greatly appreciated (if you need a copy of the code (it's an Adobe Flash Builder 4.7 project - I have made a git repo - just comment if you want the link).

更新

我意识到我没有登记的图像作为光标 - 我更新了我的 Startup.as 文件,以反映变化(看看 createCustomCursor 功能) - 光标仍不能正常工作,那是应该出现的精灵依然没有出现

I realized I wasn't registering the image as the cursor - I updated my Startup.as file to reflect changes (take a look at createCustomCursor function) - the cursor still isn't working, and the sprite that is supposed to appear is still not appearing.

另外 - 以防万一你想看到 AvatarEnemy 是从哪里来的:

Also - just in case you want to see where AvatarEnemy is coming from:

AvatarEnemy.as

package Classes
{   
    import starling.display.Image;
    import starling.display.Sprite;
    import starling.textures.Texture;

    public class AvatarEnemy extends Sprite
    {
        [Embed(source='Enemypic.png')]
        private static var Enemypic:Class;

        private var texture:Texture = Texture.fromBitmap(new Enemypic());

        private var image:Image = new Image(texture);

        public function AvatarEnemy() 
        {               
            image.x = 0;
            image.y = 200;

            addChild(image);            
        }
    }
}

更新

我解决了,为什么精灵消失了 - 我需要把八哥初始化code( mStarling.start() mStarling =新的八哥(游戏,舞台); )上面所有的 stage.something 行。我编辑code,以反映我在 Startup.as 一样。我还需要将光标虽然帮助。

I resolved why the sprite was disappearing - I needed to put the starling initialize code (mStarling.start() and mStarling = new Starling(Game, stage);) above all the stage.something lines. I edited code to reflect what I did in Startup.as. I still need help with the cursor though.

推荐答案

我一直在践行着你的code和这一切似乎不错,但香港专业教育学院使用的是不同的图像被,这样就必须成为问题。

I have been practicing with your code and it all seems fine, but ive been using a different image, so that must be the problem.

确认图像的尺寸为32×32以下,这就是一个光标图像的最大尺寸,否则操作系统将不会接受它。

Make sure the size of the image is below 32x32, thats the max size of an cursor image, otherwise the OS won't accept it.

这篇关于德裕库 - 当我尝试改变光标移到图片...它不工作(从舞台精灵元素自败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆