0
votes

I'm currently trying to create multiple textfields that are drag and droppable. I was following a tutorial earlier which used quads as an example where you would use:

var target:Quad = event.target as Quad;

which targeted the quad that you were hovering over, I tried to change it to

var target:TextField = event.target as TextField;

where upon compiling gave me the error of "Cannot access a property or method of a null object reference." I'm not quite sure what the problem is so if someone could clear this up for me that would be great.

Here is the rest of the code thats relevant:

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);

            }

The ontouch function:

        //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;
            }
1
are you sure that this casting does not cause a null target object? var target:starling.text.TextField= event.target as starling.text.TextField - Adrian Pirvulescu
I'm not quite sure what you mean, could you elaborate? - David
I forgot to mention that the error occurs when I try to move the textField and not immediately after launch - David
you posted "Cannot access a property or method of a null object reference." error message. Where does this occur? can you put a breakpoint in the handler function at the line where you read the target from the event and cast it, and check if the target variable is not null ? I see no other place where you could get such an error. - Adrian Pirvulescu
It occurs on this line "target.x += delta.x;" - David

1 Answers

0
votes

try

    //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;
        }