It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Flash Forum

anyone have a code that makes an object move in 8 directions

Moderator: Phate

anyone have a code that makes an object move in 8 directions

Postby Solar42693 on Wed Jul 12, 2006 11:32 pm

i've tried 3 or 4 but i couldn't get any to work right

thanks
User avatar
Solar42693
 
Posts: 35
Joined: Tue Jul 11, 2006 12:56 am

Postby stewball on Sat Jul 15, 2006 1:13 am

Explain it better and I may be able to help you, I normally can as long as I know what you're talking about.
User avatar
stewball
100+ Club
 
Posts: 116
Joined: Sun Oct 30, 2005 6:30 pm
Location: London, UK

Postby Solar42693 on Sat Jul 15, 2006 3:48 pm

Explained Better: Do you have an actionscript code i can apply to a movieclip to make it rotate, face, and move in 8 directions by using the arrow keys, up to go forward in the direction your facing, right to rotate to the right, left to rotate to the left, and down to go backwards from the direction your facing. Like a spaceship or something.
User avatar
Solar42693
 
Posts: 35
Joined: Tue Jul 11, 2006 12:56 am

Postby Solar42693 on Sat Jul 15, 2006 3:51 pm

For example, the spaceship in this game http://addictinggames.com/asteroidsduel.html
User avatar
Solar42693
 
Posts: 35
Joined: Tue Jul 11, 2006 12:56 am

Postby stewball on Sat Jul 15, 2006 5:48 pm

Although I'd rather write a post walking you through how to do this, I've never tried flash game programming so I'm not too sure myself but a quick google around got me:

http://www.adobe.com/devnet/flash/artic ... roids.html

That should contain what you were after.

I don't mean any offence here but you seem pretty new to flash and actionscript, I recommend getting a book or doing beginner tutorials for the basics rather than just copying and pasting code and not fully knowing what all of it does.
User avatar
stewball
100+ Club
 
Posts: 116
Joined: Sun Oct 30, 2005 6:30 pm
Location: London, UK

Postby Solar42693 on Sun Jul 16, 2006 12:17 am

Thanks for the site, and about the beginner tutorials thing, I've done most of those. It's moving on to the intermediate tutorials that's the problem lol.
User avatar
Solar42693
 
Posts: 35
Joined: Tue Jul 11, 2006 12:56 am

Postby Solar42693 on Sun Jul 16, 2006 5:36 pm

After cutting down the code from that website here's the code for anyone else that wants it. Just create a movieclip and give it an instance name of ship.
Code: Select all
function makeShip (){
   
   ship.angle = 0;    
   ship.xv = 0;       
   ship.x = 0;       
   ship.y = 0;       
   ship.yv = 0;       
   ship.xa = 0;       
   ship.ya = 0;       
   ship.v = 0;       
   ship.visc = 0.02;    
   ship.drag = 0;       
   
   
   mp = Math.PI, mpow = Math.pow, msr = Math.sqrt;
   
   ship.onEnterFrame = function() {
      
      if (Key.isDown(Key.RIGHT)) {
         this.torque = 5;
      } else if (Key.isDown(Key.LEFT)) {
         this.torque = -5;
      } else {
         this.torque = 0;
      }

      this.rtorque = 0.5*this.AngVelocity;
      this.AngAcceleration = this.torque-this.rtorque;
      this.AngVelocity += this.AngAcceleration;
      this.angle += this.AngVelocity;

      if (this.angle<0) {
         this.angle += 360;
      }
      if (this.angle>360) {
         this.angle -= 360;
      }

      this.v = msr(mpow(this.xv, 2)+mpow(this.yv, 2));
      this.drag = this.visc*this.v;

      
      if (Key.isDown(Key.UP)) {
         this.force = 1;
      } else {
         this.force = 0;
      }
      this.xa = ((this.drag*this.xv)*-1)+(this.force*Math.sin(mp/180*this.angle));
      this.ya = ((this.drag*this.yv)*-1)-(this.force*Math.cos(mp/180*this.angle));
      this.xv += this.xa;
      this.yv += this.ya;
      this._y += this.yv;
      this._x += this.xv;
      this._rotation = this.angle;

   };
}

function Game() {
   
   doo = new makeShip();
   
}

foo=new Game();



Apply this to the actionscript on the keyframe, not on the actual mc.
User avatar
Solar42693
 
Posts: 35
Joined: Tue Jul 11, 2006 12:56 am


Who is online

Users browsing this forum: No registered users and 0 guests