A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [F8] Why oh Why does this not work?

  1. #1
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156

    [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=0i<5i++){
                
    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?
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    can you use absolute path instead of relative ?
    ~calmchess~

  3. #3
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    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);
            }
        }

    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  4. #4
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    bumpity bump bump
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center