actionscriptblackberry-playbookdynamic-class

How to override a function in a dynamic class with actionscript?


This is for a playbook app.

I have two classes:

public dynamic class Bullet extends Sprite {
   public function update():void {
      x += 5;
      y += 5;
   }
}

public class BulletFactory {
   public function createFastBullet:Bullet {
      var result:Bullet = new Bullet();

      result.update = function() {
         x += 10;
         y += 10;
      }
   }
}

This is essentially what I'm trying to do. What's the best way to achieve this in actionscript?


Solution

  • You could solve your issue using a combination of Interfaces, extensions and overrides. The below won't compile, but should give you a starting point.

    public interface IBullet {
        function update():void
    }
    
    public class DefaultBullet extends Sprite implements IBullet{
       public function update():void {
          x += 5;
          y += 5;
       }
    }
    
    public class FastBullet extends DefaultBullet implements IBullet{
       override public function update():void {
          x += 10;
          y += 10;
       }
    }
    
    public class BulletFactory {
       public function createBullet(bulletType:String):IBullet {
          var bullet:IBullet;
          if(bulletType=="fast"){
            bullet = new FastBullet();
          }else{
            bullet = new DefaultBullet();
          }
          return bullet;
       }
    }