Flash ActionScript help

Discussion in 'Mac Programming' started by OneTraveler, May 6, 2007.

  1. OneTraveler macrumors member

    Joined:
    Oct 16, 2002
    Location:
    End of the Earth
    #1
    OK, this probably isn't difficult at all, but right now the solution is eluding me. I'm helping someone try to make their first flash game (of course, it's new for me too!), and we've hit a small hurdle. Everything is great except for one mistake we may have made early on. Here's the issue: we've created a "tank"symbol that is comprised of a few other symbols, one of which being the "gun". In the gun symbol is a timeline that has a bullet leaving the barrel and traveling off the stage. Our dilemma is that now the bullet is forever tied to the orientation of the gun symbol. If we fire the bullet and then move the tank symbol the bullet shifts as well.

    Is there anyway to script the bullet to "forget" that it's associated with the gun once it's been fired? This would solve two issues for me: a) the bullet moving unexpectedly, and b) will also allow me to fire more than one bullet at a time (currently, if I fire immediately after the bullet leaves the gun, the previous bullet will disappear an be replaced by the new one).

    I can't wait to hear what you guys can come up for me.
     
  2. angelneo macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #2
    To simplify matters, remove the bullet sprite from the tank sprite, this way you can attach as many bullets sprite as possible, and they would be independent of your tank sprite. Use attachMovie or DuplicateMovieClip to help you.

    Also, you can use mathematical equations to help you describe the motion of your bullet sprite. (This is when those effort spend in physics and maths class pays off)
     
  3. bmb012 macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    Yeah, that's pretty much what you have to do, delete your bullet from the tank barrel, right click on the shell in your library, select linkage, if make sure it's something simple like 'bullet,' and leave the two checkmarks checked.

    When the tank fires, attach that bullet, set the rotation to the rotation of the barrel, and the _x and _y to the _x and _y of the tank.

    Ah and here's the math for finding direction from rotation:

    LeftRight = speed * Math.cos((rotation)*(Math.PI/180))
    UpDown = speed * Math.sin((rotation)*(Math.PI/180))

    definitely a formula no coder should ever be without.
     
  4. OneTraveler thread starter macrumors member

    Joined:
    Oct 16, 2002
    Location:
    End of the Earth
    #4
    Ok, so you sound like the folks I need to talk to. If I could bother you for a little more info.....

    a) in your response:
    "LeftRight = speed * Math.cos((rotation)*(Math.PI/180))"...which part of that do I fill in (silly question, I'm sure...but it's new to me)

    b) So if I have a symbol called TANK - made of a firing button, and a GUN that follows the mouse...inside the GUN symbol is an animation of the gun firing with an explosion and currently the bullet that is causing my problem, where should I apply the AS you supplied (after I remove the bullet from it's current location, of course.)

    Thank you so much for your time....really.
     
  5. angelneo macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #5
    a) To understand bmb012 equation, It comes from the trigonometry formula using a right angle triangle. The speed is the hypotenuse, (rotation)*(Math.PI/180) is the angle (converting radian), multiple them with the Cosine of the angle, you will get adjacent length (which is "LeftRight"), same goes for Sine.

    http://math2.org/math/algebra/functions/sincos/overview.htm

    This simple diagram should be able to show you more clearly.

    b) I'm not sure how you code your firing, but for example, the part when you detect the user activate a gunfire.

    Code:
    on (press) {
        //wherever the clip you uses as the canvas
        _root.(mc name).attachMovie("lib id name", "newBulletName"+number,number);
        eval("_root.(mc name).newBulletName"+number).onEnterFrame = function() {
            //code to describe the motion over time.
        }
    }
    
    Do read up on the attachMovie api in the help files to understand how it works.

    EDIT: fyi, there are a lot of peusdo code on the internet to help you plot bullet trajectory.
     

Share This Page