Skip to main content
markerline
Inspiring
July 18, 2013
Answered

simple error #1034, forgot how to solve this.

  • July 18, 2013
  • 1 reply
  • 2054 views

I have the following code:

import flash.events.Event;

import flash.display.MovieClip;

var speed:int=100;

var arr:Array=new Array();

    var s:String=new String();

    var m:MovieClip=new MovieClip();

addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);

addEventListener(Event.ENTER_FRAME,onEnterFrameHandler2);

function onEnterFrameHandler(e:Event):void{

    d0.x=Math.sin(90+speed)+speed;

    speed+=1;

    d0.y=0;

}

function onEnterFrameHandler2(e:Event):void{

   

    for (var i:int=1;i<100;i++){

        trace(i);

        s="w"+i;

        //s=s.toString();

        arr.push(s);

        trace(s);

        m=arr;

        m.x=Math.sin(90*i);

        m.y=m.x;

        trace(m);

    }

}

-----------------------------------------

TypeError: Error #1034: Type Coercion failed: cannot convert "w1" to flash.display.MovieClip.

    at WDVM_Flash2_fla::MainTimeline/onEnterFrameHandler2()

-------------------------------------------

I am trying to get an array of objects named w1 through w"X" to move dynamically.  I've tried to set the Movie Clip "m" to an array element but apparently this is what throws the error.  How do I fix this properly?  Thanks in advance.

This topic has been closed for replies.
Correct answer kglad

try:

import flash.events.Event;

import flash.display.MovieClip;

var speed:int=100;

var arr:Array=new Array();

var X:int = 100;  //?

for(var i:int=1;i<=X;i++){

arr.push(MovieClip(getChildByName("w"+i)));

}

addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);

addEventListener(Event.ENTER_FRAME,onEnterFrameHandler2);

function onEnterFrameHandler(e:Event):void{  // not clear what this is supposed to do

    d0.x=Math.sin(90+speed)+speed;

    speed+=1;

    d0.y=0;

}

function onEnterFrameHandler2(e:Event):void{

    for (var i:int=0;i<arr.length;i++){

        arr.x=Math.sin(90*i); // not clear this is correct.  do you want to multiply by speed?

        arr.y=arr.x; // doesn't look correct.

    }

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 18, 2013

try:

import flash.events.Event;

import flash.display.MovieClip;

var speed:int=100;

var arr:Array=new Array();

var X:int = 100;  //?

for(var i:int=1;i<=X;i++){

arr.push(MovieClip(getChildByName("w"+i)));

}

addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);

addEventListener(Event.ENTER_FRAME,onEnterFrameHandler2);

function onEnterFrameHandler(e:Event):void{  // not clear what this is supposed to do

    d0.x=Math.sin(90+speed)+speed;

    speed+=1;

    d0.y=0;

}

function onEnterFrameHandler2(e:Event):void{

    for (var i:int=0;i<arr.length;i++){

        arr.x=Math.sin(90*i); // not clear this is correct.  do you want to multiply by speed?

        arr.y=arr.x; // doesn't look correct.

    }

}

markerline
Inspiring
July 18, 2013

Hi kglad.  thank you so much for contributing.  I had forgotten how to properly cast the array variable which is a string plus number to a MovieClip and it is with the getChildByName option.  The areas where you were confused was just me experimenting with sine wave motion.  Nothing too exciting.  I actually got an interesting version of my file when I properly coded the motion but the getChildByName was what I needed for this.  Thank you!