A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Enemy movement

  1. #1
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693

    Enemy movement

    Hi, I'm picking back up on a space shooter I was making a while ago, and I just can't seem to get this one part down... this is actually why i stopped, so hopefully someone can help. I have been working on enemy movement and I have a few different movement types down but I can't seem to get curved movement down. attached is a diagram of what I'm trying to achieve, roughly, I can't even draw a good curved line but I think you get the idea...

    the one on the left is easy, just
    Code:
    this._y+=this.speedY+bgSpeed;
    this._x+=this.speedX;
    with some if statements for when to change the x direction but i can't seem to get a seemless curve down... Any help, hints, ideas would be of great use. I tried the following code which is a complete flop but I'm including it so you know I've tried things and am not just lazily asking for someone to do it for me:

    Code:
    if(this.moveType==3){
    			
    			if(this._x>=this.startX){
    				this.dFrmStart=this._x-this.startX;
    			}else if(this.startX>this._x){
    				this.dFrmStart=this.startX-this._x;
    			}		
    			if(this.dFrmStart>this.maxDist){
    				this.speedX=this.speedX*(-1);
    			}
    			
    		if(this.dFrmStart==0){
    			this.dFrmStart=.01;
    		}
    			this.speedMod = (this.dFrmStart/this.maxDist);
    			this.speedMod = 1 - this.speedMod;
    			if(this.speedMod<0.1){
    				this.speedMod=0.1;
    			}
    			if(this.speedMod==0){
    				this.speedMod=1;
    			}
    			
    			this._y+=(this.speed)+bgSpeed;
    			this._x+=this.speedX*this.speedMod;
    				
    		}
    anyways, thanks in advance...

    ChaseNYC
    Attached Images Attached Images
    mmm signature

  2. #2
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    try using a sine wave, for example: if you wanted your enemy to come straight down from the top:

    _y+=speed;
    _x=wobble*Math.sin(_y);
    didn't give it much thought, but i think that works

    hehe, probably simpler than anything you could have imagined

    note: sine waves aren't perfectly circular, but it'll do as a movement type, as it still curves nicely, will this do then?

    other other note: ooh, try Math.tan too, that should give you a pretty cool movement type as well
    Last edited by trogdor458; 11-06-2006 at 12:09 AM.

  3. #3
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    yeah ummm, couldn't get that to work... i tried a few variations all similar to this:

    Code:
    			this._y += this.speed+bgSpeed;
    			this._x += this.speedX*Math.sin(this._y);
    none of them work... i just end up with either weird girating spaceships or no movement really... maybe i'm missing what wobble is?
    mmm signature

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    sine and cosine baby

    sine for y, cosine for x

    you need a size to ocilate at and a angle, play around with different combinations. REMEMBER YOUR DEALING WITH RADIANS, 0 - Math.PI is the same as saying 0 - 180

    angle += speed

    var vx = size * Math.cos(angle)

    _x += vx
    lather yourself up with soap - soap arcade

  5. #5
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    lol, i guess i'm gonna just have aliens who don't drive well and have no interest in making curved turns... thats all greek to me... i give up with this curved movement stuff...

    ... fine, i'm just tired, i'm not giving up, but i can't try to figure that out till tomorrow morning... thanks for the tips though guys...
    mmm signature

  6. #6
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    or maybe i didnt think enough...
    let me think heavier and with a calculator!!
    hmm...should work, let me try my computer...yup, works
    first, keep in mind that a sine wave looks like your ziz-zag motion, but with the corners rounded, try messing around with some additional variables:

    _y+=fallspeed;
    _x = wobble*Math.sin(_y/wobblespeed)+initdistance;
    fallspeed sets the how fast your enemy will go down the screen
    wobble will set the maximum distance your enemy will move on the x plane (and minimum as well)
    wobblespeed will set how fast your ship will wobble (the larger the number, the slower the wobble)
    initdistance will set the distance from the left side of the screen your object will be

    http://trogdorffe.tripod.com/hoorah.swf

    worx, see? i used 5 for fallspeed, 50 for wobble, 10 (or wait, was it 5?) for wobblespeed, and 100 for initdistance

    3 lines of code, one movieclip (i feel like i set a record)
    hell, i think even the fallspeed and wobble are pixelbased

    thats all greek to me...
    im pretty sure i speak english...

    Quote Originally Posted by mr malee
    cosine for x
    actually, cosine is very similar to sine, if you were to replace Math.sin with Math.cos, you couldn't tell the difference!!
    Last edited by trogdor458; 11-06-2006 at 12:47 AM.

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