Seth Johnson
Seth Johnson

Reputation: 21

Actionscript 3 no compiler errors, output is freaking out though

So I'm fairly new to actionscript 3 and to practice I made a script that when one of the three squares in the array is clicked it will move it to a random spot. there arent any compiler errors, but the output says:

ReferenceError: Error #1069: Property x not found on String and there is no default value.
    at trying_Scene1_fla::MainTimeline/move_sq()

when one of the squares is clicked. Heres the script:

import flash.events.MouseEvent;
import flash.events.Event;


var squares:Array=[ square_1, square_2, square_3]
var low:Number=1;
var high:Number=100;
var chosen:Number=Math.floor(Math.random()* (1+ high - low))+low;

for(var i=0; i<squares.length; i++){
    squares[i].addEventListener(MouseEvent.CLICK, move_sq);
}

function move_sq(e:MouseEvent):void{
    var square_num = e.target.name;
    if (chosen>50) {
        square_num.x -= Math.random()* 10
        square_num.y -= Math.random()* 10
    }
    else {
    square_num.x += Math.random()* 10
    square_num.y += Math.random()* 10
    }
}
enter code here

i hope its just a small mistake or something i didnt know about, if you can help please do. Thanks!

Upvotes: 2

Views: 81

Answers (2)

Mario Pozzo
Mario Pozzo

Reputation: 11

You can fix the error by doing this in the mouse listener funcion:

var square_num = e.target;

but you can do this, if you know the type:

var square_num:MovieClip = MovieClip(e.target);

Upvotes: 1

mitim
mitim

Reputation: 3201

It's because "square_num" is a string:

var square_num = e.target.name;

The name property of e.target is a string, which do not have x and y properties. I think you perhaps want to use:

var square_num:MovieClip = (MovieClip) (e.target.name);

(I am guessing "square_1", "square_2", and "square_3" are movieclips)

Upvotes: 0

Related Questions