Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

1067: Implicit coercion of a value of type String to an unrelated type flash.display:DisplayObject.

Participant ,
Apr 06, 2010 Apr 06, 2010

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

TOPICS
ActionScript
4.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 06, 2010 Apr 06, 2010

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

...
Translate
Community Expert ,
Apr 06, 2010 Apr 06, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 06, 2010 Apr 06, 2010

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???

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2010 Apr 06, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 06, 2010 Apr 06, 2010

Thanks, I'll read up on that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2010 Apr 06, 2010
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines