-
Developing For Dunkets
[F8] Why oh Why does this not work?
I made a small class, or I'm making the class anyway, and I can't seem to get this right. Here's the class, its short now:
PHP Code:
import mx.utils.Delegate;
class Enemy{
private var en:MovieClip;
private var i:Number;
private var maxEnemies:Number = 5;
private var enemies:Number = 0;
function Enemy(){
for(i=0; i<5; i++){
en = _root.attachMovie('enemy1','enemy'+i, _root.getNextHighestDepth(),{_x:Stage.width+30,_y:Math.random()*Stage.height-10});
en.onEnterFrame = Delegate.create(en,moveEnemy);
}
}
private function moveEnemy(){
trace(this);
this._x -= 10;
if(this._x <= 0-this._width){
this.removeMovieClip();
}
}
}
I'm using Delegate to pass the scope of the attached movieclip to my moveEnemy function. When I trace this I get enemy1, enemy2... and so on. And, when I trace typeof(this) I get movieclip. But I get an error that says that 'this' has no _x property or _width property. How is it that this refers to my enemy movieclips but they have no properties? And, anyone know how to get around this?
-
Senior Member
can you use absolute path instead of relative ?
-
Developing For Dunkets
if you mean calling enemy0 instead of this inside of my moveEnemy function then yes. I actually already got farther with this class and had to just scrap passing the scope to that function that way. But, I'd still like to know why it is that that happens and why it that first example isn't working. Here is what I have now that is semi working:
PHP Code:
import mx.utils.Delegate;
class Enemy{
public var runOffStage:Function;
public var addListener:Function;
private var en:MovieClip;
private var i:Number;
private var maxEnemies:Number = 5;
private var enemies:Number = 0;
private var enInterval:Number;
function Enemy(){
AsBroadcaster.initialize(this);
this.addListener(this);
enInterval = setInterval(Delegate.create(this,moreEnemies),Math.random()*4000);
en = _root.attachMovie('enemy1','enemy'+enemies, _root.getNextHighestDepth(),{_x:Stage.width+30,_y:Math.random()*Stage.height-10});
moveEnemy(en,this);
}
function moveEnemy(en:MovieClip,Event){
en.onEnterFrame = function(){
this._x -= 10;
if(this._x <= 0-this._width){
Event.broadcastMessage("runOffStage");
this.removeMovieClip();
delete this.onEnterFrame;
}
}
}
function moreEnemies(){
this.enemies++;
trace(this.enemies);
en = _root.attachMovie('enemy1','enemy'+this.enemies, _root.getNextHighestDepth(),{_x:Stage.width+30,_y:Math.random()*Stage.height-10});
moveEnemy(en,this);
if(enemies == maxEnemies){
clearInterval(enInterval);
}
}
}
-
Developing For Dunkets
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|