Question

Im following this tutorial,http://www.flash-game-design.com/flash-tutorials/dynStar-flash-tutorial.html. And i keep receiving errors of unidentified methods,

Here is my Code,

 var stars = 100;
var maxSpeed = 16;
var minSpeed = 2;

for(var i = 0; i<stars; i++){
    var mc = this.attachMovie("star", "star"+i,i);
    mc._x = random(Stage.width);
    mc._y = random(Stage.height);

    mc.speed = random(maxSpeed-minSpeed)+minSpeed
    var size = random(2)+0.6*(random(4));
    mc._width = size;
    mc._height = size;
}

this.onEnterFrame = function(){
    for(var j=0; j<stars;j++){
        var mc = this["star"+j];
        if (mc._y>0){
            mc._y -= mc.speed ;
        } else{
            mc._y = stage.height;
            mc.speed = random(maxSpeed-minSpeed)+minSpeed
            mc._x = random(Stage.width);
        }

    }
}
Was it helpful?

Solution

Perhaps you have not correctly linked your movieclip in your library to have an instance/identifier name of "star" in which case look at the first part of that tutorial.

The other possibility is that the method "random" has actually been deprecated, so depending on your version of Flash player maybe it isn't working. Try Math.random instead.

OTHER TIPS

Actually the issue is that that tutorial is AS2, and a lot of the code doesn't translate to as3. The biggest things are replacing "random" with Math.random(), attachMovie & event.enterFrame with a DisplayObjectContainer class, and _x&_y with just x & y.

I have updated most of the code, my only issue is that i'm not sure what to do with this line:

var mc = this.attachMovie("star", "star"+i,i);

I know I need to create a variable and addChild, but i'm not sure how it applies with generating multiple movie clips from one. I thought addChild only worked for 1 thing. SO any help on how to fix this to work would be great. Just trying to teach myself as I go.

updated so far to:

this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

import flash.display.MovieClip;
import flash.events.Event;

var stars = 100;
var maxSpeed = 16;
var minSpeed = 2

for( var i = 0; i<stars; i++)
{
    var mc = this.attachMovie("star", "star"+i,i);
    mc.x = Math.random() * stage.stageWidth;
    mc.y = Math.random() * stage.stageHeight;
    mc.speed = Math.random() * (maxSpeed-minSpeed)+minSpeed
    var size = Math.random() * 2+(0.6*(Math.random() * 4));
    mc.width = size;
    mc.height = size;
}

function enterFrameHandler(event:Event):void
{
    var target:MovieClip = MovieClip(event.target);
    for (var j = 0; j<stars;j++)
    {
        var mc = this.("star"+j);
        if (mc.y>0)
        { 
            mc.y -= mc.speed;
        }
        else
        {
            mc.y = stage.stageHeight;
            mc.speed = Math.random() * (maxSpeed-minSpeed)+minSpeed
            mc.x = Math.random() * stage.stageWidth;

        }
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top