【问题标题】:Starling textField touch eventStarling textField 触摸事件
【发布时间】:2013-04-15 08:58:26
【问题描述】:

我目前正在尝试创建多个可拖放的文本字段。我之前正在关注一个教程,该教程使用四边形作为您将使用的示例:

var target:Quad = event.target as Quad;

针对您悬停的四边形,我尝试将其更改为

var target:TextField = event.target as TextField;

编译时出现“无法访问空对象引用的属性或方法”的错误。我不太确定问题出在哪里,所以如果有人可以为我解决这个问题,那就太好了。

以下是其余相关代码:

public function onAdded():void{
//stuff that initialises bitmap

for(var i:int=0; i<3; i++){

            //create Textfield
            var bmpFont:starling.text.TextField = new starling.text.TextField(100,100, "test", "Arial", 10);

            bmpFont.fontSize = 50;
            if(i==0){
                bmpFont.color = Color.WHITE;

            }
            else if (i==1){
                bmpFont.color = Color.RED;  
            }

            else if (i==2){
                bmpFont.color = Color.BLUE; 
            }

            bmpFont.x = Math.random() * (stage.stageWidth - bmpFont.width);
            bmpFont.y = stage.stageHeight/2;
            //centering pivot point
            bmpFont.pivotX = 50;
            bmpFont.pivotY = 50;

            //centering code
            //bmpFont.x = stage.stageWidth - bmpFont.width >> 1;
            //bmpFont.y = stage.stageHeight - bmpFont.height >> 1;

            useHandCursor = true;                   
            bmpFont.addEventListener(starling.events.TouchEvent.TOUCH, onTouch);
            parent.addChild(bmpFont);

            }

ontouch 功能:

        //function activating on touch
        public function onTouch(event:starling.events.TouchEvent):void{
            var touches:Vector.<Touch> = event.getTouches(stage, TouchPhase.MOVED);
            //var target:Quad = event.target as Quad;

            var target:starling.text.TextField= event.target as starling.text.TextField

            //single finger manipulations
            if(touches.length == 1){
                var delta:Point = touches[0].getMovement(parent);
                target.x += delta.x;
                target.y += delta.y;
            }

【问题讨论】:

  • 你确定这个转换不会导致空目标对象吗? var target:starling.text.TextField= event.target as starling.text.TextField
  • 我不太清楚你的意思,你能详细说明一下吗?
  • 我忘了提到错误是在我尝试移动 textField 时发生的,而不是在启动后立即发生
  • 您发布了“无法访问空对象引用的属性或方法”。错误信息。这发生在哪里?您可以在从事件读取目标并强制转换它的行的处理函数中放置一个断点,并检查目标变量是否不为 null ?我看不到其他地方会出现这样的错误。
  • 它出现在“target.x += delta.x;”这一行

标签: actionscript textfield


【解决方案1】:

试试

    //function activating on touch
    public function onTouch(event:starling.events.TouchEvent):void{
        var target:starling.text.TextField= event.target as starling.text.TextField;
        var touches:Vector.<Touch> = event.getTouches(target, TouchPhase.MOVED);


        //single finger manipulations
        if(touches.length == 1){
            var delta:Point = touches[0].getMovement(target.parent);
            // delta and target should be not null

            target.x += delta.x;
            target.y += delta.y;
        }

【讨论】:

  • 它不再出现错误,但没有一个 TextFields 移动
  • 我做了一个跟踪,它似乎没有检测到任何一根手指的触摸,不太确定为什么它没有检测到它,因为当我使用四边形示例时,它只有“var target: Quad = event.target 作为 Quad;"似乎可以很好地检测到触摸元素
  • 那么这意味着问题出在计算增量的时间。使用断点并查看 delta.x 和 delta.y 的值是什么。还可以尝试计算 touches[0].getMovement(this) 或 getMovement(stage),抱歉,我无法调试您的代码。我可以看到的是,您将 Starling 帮助页面中的原始示例弄乱了一点。
  • 您是否启用了多点触控。这是与quad相同的项目还是新项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多