Skip to main content
Participating Frequently
August 4, 2009
Question

anyone know anything about

  • August 4, 2009
  • 1 reply
  • 281 views

Morning all,

I'm dipping my toe into the world of ASscripts, trying to do things the better way-at least i'm told, but am having great difficulty. Any pearls of wisdom you can share would be very gratefully recieved!

What i'm trying to do is assign a script to several movieClip instances, and make that bounce when it hits another movieClip on satge. I guess basic collision really. But its not working, mainly as, i think, i have no idea how to make the script read things from the stage.

This is what i have so far:

package{
  import flash.display.MovieClip;
  import flash.events.Event;
  import Wall; //movieClip linked from swf file - do i need to do this differently?
 
  public class MoveCharcater extends MovieClip{
     
      public function MoveCharcater(){
          addEventListener(Event.ENTER_FRAME, shiftChr);
    }
     
    function shiftChr(event:Event):void{
        var xDir:Number = 5;
        var yDir:Number = 5;
       
        if(this.hitTestObject(Wall)){ // not convinced this works either?!
            xDir *= 1;
            yDir *= -0.5;
        }
        this.x += (xDir + xDir);
        this.y += yDir;
    }
  }
}

Any ideas?!

This topic has been closed for replies.

1 reply

Inspiring
August 4, 2009

Here is what works. Of course you will need to write desired behavior yourself. Refined trigonometry based calculations will make it more interesting.

Class MoveCharacter. I made it a circle.

package  
{
     import flash.display.DisplayObject;
     import flash.display.Graphics;
     import flash.display.Sprite;
     import flash.events.Event;

     public class MoveCharacter extends Sprite 
     {
          // reference to the wall or other obstacle
          // set below in the corresponding accessor
          private var _obstacle:DisplayObject;
          // declare all the parameters in the header
          private var _xDir:Number = 1;
          private var _yDir:Number = 1;
          private var _xSpeed:Number = 5;
          private var _ySpeed:Number = -1;

          public function MoveCharacter () 
          {
              // your object goes here - for now it is a ball
              var gr:Graphics = this.graphics;
              gr.beginFill(0x0000FF);
              gr.drawCircle(0, 0, 10);
              gr.endFill();
              // initiate when added to stage
              addEventListener(Event.ADDED_TO_STAGE, onAdded);
          }

          private function onAdded(e:Event):void 
          {
              // listener is not needed any longer
              removeEventListener(Event.ADDED_TO_STAGE, onAdded);
              // start move
               addEventListener(Event.ENTER_FRAME, onEnterFrame);
          }

          private function onEnterFrame(e:Event):void 
          {
               if(this.hitTestObject(_obstacle)){ 
               _xSpeed = -_xSpeed;
                _ySpeed = -_ySpeed;
               }
               this.x += _xDir * _xSpeed;
               this.y += _yDir * _ySpeed;
               }
               // pass reference to the obstacle
          public function set obstacle(dispObj:DisplayObject):void {
                _obstacle = dispObj;
          }
     }

}

Inside DisplayObject where you instantiate your character and place the wall:

// Instance of obstacle
var wall:Wall = new Wall();
wall.x = stage.stageWidth - 50;
wall.y = 20;
addChild(wall);

// Instance of moving object              
var ball:MoveCharacter = new MoveCharacter ();
ball.x = 20;
ball.y = 200;
// pass reference of the obstacle to the moving object
ball.obstacle = wall;
addChild(ball);