A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Action Script help!

  1. #1
    Junior Member
    Join Date
    Mar 2004
    Posts
    27

    Action Script help!

    i put this under actionscript instead of games because it fits this category better.

    I came up with some code to ad friction to the puck. The prblem is I can only get it to move on the y axis. I am not sure on how to go about moving it on the x and y.


    code:

    ymov = 10;
    friction = .95;
    _root.onEnterFrame = function() {
    ymov *=friction;
    ball(_root.puck)._y+=ymov;
    }




    I also have the paddle constrained to a rectangle with a startdrag function.

    I just need help with moving the ball on the x and y axis and possibly inersha (for example, if the paddle hits the puck hard than the puck will go faster).

    I have the hitTests working great. I have them checking inbetween frames so the puck wont pass right through the paddle. anyways. please help

    Thanks

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Are you paraphrasing your script? Because

    ball(_root.puck)._y

    doesn't look valid to me... UNLESS you have a function called ball() which returns the movie you are intending to modify. Did you mean this?

    _root.puck._y

    At any rate, you can get movement on both axes by also having an xmov and modifying _x in the same way.

    For a good example of multiple colliding objects with friction in action, see my superball script:

    Jim's Super Balls

    In my version, I use the variables vx and vy to represent velocity - this is the same as your 'ymov'. If you zero out the gravK (gravity) constant, you'll have something you can use for Hockey or Billiards.

    - Jim

  3. #3
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    im sorry. this is what I have
    code:

    ymov = 19;

    decay = .98;
    decay2= -.98;

    _root.onEnterFrame = function() {
    ymov *= decay;
    puck._y += ymov;

    if (paddle.hitTest(_root.puck)) {
    ymov *= decay2;
    puck._y += ymov;
    }



    if (puck.hitTest(_root.tw)) {

    ymov *=decay2;
    puck._y += ymov;

    }

    };


  4. #4
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    here is the fla

    flash mx
    Attached Files Attached Files

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Your script is functionally equivalent to this, which more accurately represents what is going on - you were mixing up ricochets with damping by using a negative damping factor.

    Code:
    ymov = 19;
    decay = .98;
    
    _root.onEnterFrame = function() 
    {
      // reverse y velocity if we hit something
      if (puck.hitTest(paddle) ||
          puck.hitTest(_root.tw))      
      {
          ymov = -ymov;
      }
    
      // slow down over time
      ymov *= decay;
    
      // add velocity to position
      puck._y += ymov;
    };
    As you noted, you are currently not doing any thing with the x coordinate.
    You are also not currently tracking the velocity of the paddle, which you will need to do if the paddle is going to confer energy to the puck.

    In my superball script, which I mentioned above, when you move one of the balls with the mouse, it acts as a paddle would need to act in your script. The mouse motion is tracked and used to set the velocity of the ball that is being dragged.

    Here I've modified this code for your paddle:

    Code:
    paddle.onPress = function()
    {
    	this.isDragging = true;
    	this.vx = this.vy = 0;
    	this.lmx = _root._xmouse;
    	this.lmy = _root._ymouse;
    	this.startDrag(false);
    }
    
    paddle.onRelease = function()
    {
    	this.isDragging = false;
    	this.stopDrag();
    }
    
    paddle.onEnterFrame = function()
    {
       if (this.isDragging)
       {
    	this.vx *= decay;
    	this.vy *= decay;
    	this.vx = _root._xmouse - this.lmx;
    	this.vy = _root._ymouse - this.lmy;
    	this.lmx = _root._xmouse;
    	this.lmy = _root._ymouse;
       }
    }
    Then, when the paddle hits the puck, you can use the vx,vy (paddle velocity) information to impart motion to the puck:

    Code:
     if (puck.hitTest(paddle))
     {
       puck.xmov = paddle.vx;
       puck.ymov = paddle.vy;
     }
    Change your puck movement as follows:

    Code:
      puck.xmov *= decay;
      puck.ymov *= decay;
      puck._x += puck.xmov;
      puck._y += puck.ymov;
    When you hit a horizontal wall, reverse the xmov (horizontal velocity). You are already reversing the vertical velocity (ymov) when you hit things.

    - Jim

  6. #6
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    Thank you for the help so far. I understand where you are going with this. Here is my fla. please tell me where i went wrong? I appreciate it. This is one of those things where I have to see it in order to learn it. You know? You dont have to teach me how to do it just show me how.

    this is what I have. so far

    code:

    xmov = 10;
    ymov = 10;
    decay = .98;
    puck.xmov *= decay;
    puck.ymov *= decay;
    puck._x += puck.xmov;
    puck._y += puck.ymov;

    paddle.onPress = function()
    {
    this.isDragging = true;
    this.vx = this.vy = 0;
    this.lmx = _root._xmouse;
    this.lmy = _root._ymouse;
    this.startDrag(false);
    }

    paddle.onRelease = function()
    {
    this.isDragging = false;
    this.stopDrag();
    }

    paddle.onEnterFrame = function()
    {
    if (this.isDragging)
    {
    this.vx *= decay;
    this.vy *= decay;
    this.vx = _root._xmouse - this.lmx;
    this.vy = _root._ymouse - this.lmy;
    this.lmx = _root._xmouse;
    this.lmy = _root._ymouse;
    }
    }



    I no why this doesnt work. Its missing alot of script lol. For instance moving the puck on both planes. This is my fault though. I didnt tell it too. Anyways. anymore help would be awsome.
    Attached Files Attached Files

  7. #7
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    i took you superballs script and made it only have two balls but how do i make it so that I can have 2 different sized balls on the screen? hence on being for the puck and the other for the paddle?

  8. #8
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Okay, I fixed it so you can push the puck around with the paddle, and it bounces off the edges of the table.

    You'll still need to add stuff to make the puck go into the goals (and score the game).

    Code:
    
    xmov = -19;
    decay = .98;
    
    _root.onEnterFrame = function() 
    {
      // if we hit the paddle, move the puck
      if (puck.hitTest(paddle))
      {
    	  puck.xmov = paddle.vx;
    	  puck.ymov = paddle.vy;
      }
    				   
      // reverse x velocity if we hit left or right
      if (puck._x+puck.xmov < 60 || puck._x+puck.xmov > 60+480)      
          puck.xmov = -puck.xmov;
      // reverse y velocity if we hit top or bottom
      if (puck._y+puck.ymov < 70 || puck._y+puck.ymov > 70+255)      
          puck.ymov = -puck.ymov;
    
      // basic movement
      puck.xmov *= decay;
      puck.ymov *= decay;
      puck._x += puck.xmov;
      puck._y += puck.ymov;
    };
    
    paddle.onPress = function()
    {
    	this.isDragging = true;
    	this.vx = this.vy = 0;
    	this.lmx = _root._xmouse;
    	this.lmy = _root._ymouse;
    	this.startDrag(false);
    }
    
    paddle.onRelease = function()
    {
    	this.isDragging = false;
    	this.stopDrag();
    }
    
    paddle.onEnterFrame = function()
    {
       if (this.isDragging)
       {
    	this.vx *= decay;
    	this.vy *= decay;
    	this.vx = _root._xmouse - this.lmx;
    	this.vy = _root._ymouse - this.lmy;
    	this.lmx = _root._xmouse;
    	this.lmy = _root._ymouse;
       }
    }
    - Jim

  9. #9
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    Thank you very much Jim. Your script was very helpful and I understand it surprisingly. . If I need anymore help would it be alright to IM you or something. If not its cool.

  10. #10
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Yeah, your code was already pretty close. The more you play with this stuff, the better your understanding of game physics will get.

    I don't use an IM client, but you are welcome to email me. My email address is listed at the bottom of each page on my website (link below).

    - Jim

  11. #11
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    i made a hit test for the right goal. So when the puck comes and hits it, it will add one to your score and place the puck on yourside of the center line. It works great but only when the puck hits it at slow speeds. Is there anyway to have the hit test check every half a frame instead of everyframe? or get the

    puck.radius = 9;
    puck.width = puck.radius*2;

    i dont even know if something like this is possible but yea.
    I guess it could be called detection using math. It seems to me to be more accurate.

    since its hitting a line maybee try to add the equation of a line
    y = mx+b;
    so its accurate


    also when the puck is moving at high speeds it doesnt look like its hitting the walls. is that just because its moving fast?
    Attached Files Attached Files

  12. #12
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Yeah, you don't want to be using hitTest for the goal collision test. Probably best to avoid hitTest for twitch games completely.

    In this case you want a >= test on the x coordinate so that it can overshoot when it's moving in big jumps.

    Something like:

    Code:
    if (puck._x >= rg1._x &&
        Math.abs(puck._y - rg1._y) < rg1._width/2)
      {
         // goooooooooooooooooooo-o-o-o-o-o-al!!
      }
    And yeah, the border bounces I wrote were over simplified and you're noticing that.

    Here's a minor tweak that'll help:

    Code:
    // before
    if (puck._y+puck.ymov<75 ||  puck._y+puck.ymov>75+243) 
    {
        puck.ymov = -puck.ymov;
    }
    Code:
    // after
    if (puck._y+puck.ymov<75) 
    {
        puck._y = 75;
        puck.ymov = -puck.ymov;
    }
    
    if (puck._y+puck.ymov>75+243)
    {
        puck._y = 75+243;
        puck.ymov = -puck.ymov;
    }
    Do a similar tweak for the X bounces as well (forcing the _x into the wall for one frame, so you see it).

    - Jim

  13. #13
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    The code for the pucks boundries works great now. The only problem is that hittest
    code:

    if (puck._x >= rgl._x &&
    Math.abs(puck._y - rgl._y) < rgl._width/2)
    {
    score1 += 1;
    puck._x = 250;
    puck._y = 200;
    puck.ymov = 0;
    puck.xmov = 0;

    if (score1 == 7){
    goToAndPlay(2);
    }
    }



    The hit test works great only when you hit the center point of the mc. The problem with this code is that when you use._x or ._y, Flash will only trace where you declared the center of the mc and not the whole thing. So maybee we could try making a mc (rgl) by creating it in actionscript. for example rectangle = [ t , l , r, b ] and then using it in a mathimatical hit test. saying puck._x >= rectangle. i dont know. lol. Am i at least correct about the ._x and ._y? im still new to action script but i know my trig. (I am in a trig class right now in school)
    maybee even doing something like this
    rgl = {};
    rgl._width = 10;
    rgl._height = 90;
    rgl.area = rgl._width*rgl._height;
    rgl._x = 500;
    rgl._y = 200;

    is this even pssible to do?


    and then creating the hitest so it knows all the parameters of the mc rgi. would that work or am I way off?

    anyways

    here is the new fla.

    or

    code:

    decay = .98;
    score1 = 0;
    score2 = 0;

    _root.onEnterFrame = function() {
    puck.xmov *= decay;
    puck.ymov *= decay;
    puck._x += puck.xmov;
    puck._y += puck.ymov;
    // reverse x velocity if we hit left or right
    if (puck._y+puck.ymov<79)
    {
    puck._y = 79;
    puck.ymov = -puck.ymov;
    }
    if (puck._y+puck.ymov>73+243)
    {
    puck._y = 73+243;
    puck.ymov = -puck.ymov;
    }
    if (puck._x+puck.xmov<70)
    {
    puck._x = 70;
    puck.xmov = -puck.xmov;
    }

    if (puck._x+puck.xmov>69+462)
    {
    puck._x = 69+462;
    puck.xmov = -puck.xmov;
    }
    // basic movement

    rgl._alpha = 0;
    // if we hit the paddle, move the puck
    if (puck.hitTest(paddle)) {
    puck.xmov = paddle.vx;
    puck.ymov = paddle.vy;
    }
    // if puck hits the goal line, add 1 to score and move it to the appropiate place
    if (puck._x >= rgl._x &&
    Math.abs(puck._y - rgl._y) < rgl._width/2)
    {
    score1 += 1;
    puck._x = 250;
    puck._y = 200;
    puck.ymov = 0;
    puck.xmov = 0;

    if (score1 == 7){
    //Ill add a you win and stuff like that in the second frame
    }
    }

    };
    paddle.onPress = function() {
    this.isDragging = true;
    this.vx = this.vy=0;
    this.lmx = _root._xmouse;
    this.lmy = _root._ymouse;
    //this.startDrag(false, 80, 88, 276, 307);
    this.startDrag(false);
    };
    paddle.onRelease = function() {
    this.isDragging = false;
    this.stopDrag();
    };
    paddle.onEnterFrame = function() {
    if (this.isDragging) {
    this.vx *= decay;
    this.vy *= decay;
    this.vx = _root._xmouse-this.lmx;
    this.vy = _root._ymouse-this.lmy;
    this.lmx = _root._xmouse;
    this.lmy = _root._ymouse;
    }
    };



    Thanks so much for the help. We where all newbs at one point.

    -JJ
    Attached Files Attached Files
    Last edited by Amax2; 03-25-2004 at 12:36 PM.

  14. #14
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    Your one tea cup game is so awsome. I played it forever last night. Didint get a high score though

  15. #15
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    My "one tea cup" game? Which game is that?

    I'll figure out the goal stuff later - Dr. appt...

    - Jim

  16. #16
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    haha the game was "Cheese Toast III: Attack of the Scones "
    it was fun.

    hey i tried going on my own again for the hit test and I came up with this.

    code:

    if (puck._x > 530 && puck._y > 151 && puck._y < 251)
    {
    score1 += 1;
    puck._x = 250;
    puck._y = 200;
    puck.ymov = 0;
    puck.xmov = 0;

    if (score1 == 7){
    goToandPlay(2);
    }
    }


    works fine, but now when the puck goes fast it does the hittest early like way before the goal. Well it just doesnt look like its hitting the goal.

    this was just one idea that I had. I will try more. Thanks for the help and thanks for staying with me for this long. I appreciate it alot.


    this is what you had before.

    code:

    if (puck._x >= rgl._x &&
    Math.abs(puck._y - rgl._y) < rgl._width/2)
    {
    score1 += 1;
    puck._x = 250;
    puck._y = 200;
    puck.ymov = 0;
    puck.xmov = 0;
    }


    -JJ thanks again.
    I have learned alot from you and your website.
    Last edited by Amax2; 03-25-2004 at 08:19 PM.

  17. #17
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    ....

  18. #18
    Junior Member
    Join Date
    Mar 2004
    Posts
    27
    thank you for everything that you have done to help me.
    but now your gone

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