Copy link to clipboard
Copied
Hi guys. I am building little games never anything too big. I'm having fun.
I'm getting the following error message
1067: Implicit coercion of a value of type String to an unrelated type flash.display:DisplayObject.
It's got to do with the varObject variable I pass into the hitTestObject (at the end of the code)
a. I exported the moveclip for actionscript and gave it an instance name so what's the problem???
stage.addEventListener(MouseEvent.CLICK, myClickReaction);
// speeds ALONG NYPOTENUSE
var v:Number=5;
var varObject:String;
var i:Number;
var arrayObjects:Array;
// populate array and initiate variables i and varObject
arrayObjects=["chocolate","icecream","pizza","hamburger","sandwich","egg"];
i = 1;
varObject = arrayObjects[i - 1];
trace(varObject);
// mouse click point
var clickPoint:Point = new Point();
function myClickReaction(e:MouseEvent):void {
clickPoint.x=mouseX;
clickPoint.y=mouseY;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(event:Event):void {
var xDistance:Number=clickPoint.x-sunny.x;
var yDistance:Number=clickPoint.y-sunny.y;
var angle:Number=Math.atan2(yDistance,xDistance);
sunny.x+=v*Math.cos(angle);
sunny.y+=v*Math.sin(angle);
if (clickPoint.x>=sunny.x) {
sunny.gotoAndPlay("walk right");
//trace(Math.abs(xDistance));
}
if (sunny.x>=clickPoint.x) {
sunny.gotoAndPlay("walk left");
//trace(Math.abs(xDistance));
}
if (Math.abs(xDistance)<=10) {
//trace("works");
sunny.gotoAndPlay("static");
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function enterFrameHandler(e:Event):void {
//if (sunny.hitTestObject(hotspot_pizza)) {
if (sunny.hitTestObject(varObject)) {
trace("Yeah boy");
}
}
if you have objects with references (in the current scope) that match those strings, you can use:
...
stage.addEventListener(MouseEvent.CLICK, myClickReaction);
// speeds ALONG NYPOTENUSE
var v:Number=5;
var varObject:String;
var i:Number;
var arrayObjects:Array;
// populate array and initiate variables i and varObject
arrayObjects=["chocolate","icecream","pizza","hamburger","sandwich","egg"];
i = 1;
varObject = arrayObjects[i - 1];
trace(varObject);
// mouse click point
var clickPoint:Point = new Point();
func
Copy link to clipboard
Copied
if you have objects with references (in the current scope) that match those strings, you can use:
stage.addEventListener(MouseEvent.CLICK, myClickReaction);
// speeds ALONG NYPOTENUSE
var v:Number=5;
var varObject:String;
var i:Number;
var arrayObjects:Array;
// populate array and initiate variables i and varObject
arrayObjects=["chocolate","icecream","pizza","hamburger","sandwich","egg"];
i = 1;
varObject = arrayObjects[i - 1];
trace(varObject);
// mouse click point
var clickPoint:Point = new Point();
function myClickReaction(e:MouseEvent):void {
clickPoint.x=mouseX;
clickPoint.y=mouseY;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(event:Event):void {
var xDistance:Number=clickPoint.x-sunny.x;
var yDistance:Number=clickPoint.y-sunny.y;
var angle:Number=Math.atan2(yDistance,xDistance);
sunny.x+=v*Math.cos(angle);
sunny.y+=v*Math.sin(angle);
if (clickPoint.x>=sunny.x) {
sunny.gotoAndPlay("walk right");
//trace(Math.abs(xDistance));
}
if (sunny.x>=clickPoint.x) {
sunny.gotoAndPlay("walk left");
//trace(Math.abs(xDistance));
}
if (Math.abs(xDistance)<=10) {
//trace("works");
sunny.gotoAndPlay("static");
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function enterFrameHandler(e:Event):void {
//if (sunny.hitTestObject(hotspot_pizza)) {
if (sunny.hitTestObject(this[varObject])) {
trace("Yeah boy");
}
}
Copy link to clipboard
Copied
THANKS. Works perfectly BUT I really am trying to learn and not just take people's code. What is wrong with my code - it looked perfect. What does "this" do???
Copy link to clipboard
Copied
you have to instruct flash to coerce your string to an object. you do that using the array operator: [ ]
so, if you have an object (eg, a variable var1=33), and you want to use the string "var1" to point to the variable var1, use:
trace(this["var1"]); // output=33
Copy link to clipboard
Copied
Thanks, I'll read up on that.
Copy link to clipboard
Copied
you're welcome.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more