A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Ball - Moving 90 Degrees

  1. #1
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167

    Ball - Moving 90 Degrees

    Hey again guys

    I know the title seems a bit simplistic but thats the trouble Im having. Its not that I cant do it - because I can, but the problem lies with not going directly 90 degrees

    Ill explain what I mean and give you the .fla to see for yourself



    I have this ball which I want to move in one direction by itself. It will keep moving in this direction until it makes contact with the movie clip 'boundary'.
    If the hittest is true then the ball will then rotate 90 degrees, and will continue to do so on and on and on

    Now the problem (actually two when I think about it):
    I used pretty much the same code I did for mine and Ostils cargame (theres a thread in this forum) and just modified it to work the way I needed it to.

    1) The first problem is that the ball doesnt turn a perfect 90 degrees mathematically, it just looks like it does. This is a problem because if you let it run, eventually the ball will go on angles and such

    2) When the ball hits the wall, it gives a slight bounce back, and thats a problem when it hits near a corner because it will bounce back and hit the other wall (sort of hard to explain but you can see for yourself)


    So yea, if someone could help me out with this, by showing me what Im doing wrong and how to take the bounce of my hittests it would be appreciated

    The code is in the ball movieclip - *attaches file*

    Thanks guys
    Last edited by Osteel; 05-01-2009 at 02:39 AM.

  2. #2
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    I cant see too much of a problem, i didnt notice the first one you said about at all. The second problem is because you are allowing the ball to move too far into the walls, so as soon as it turns, it needs to turn again. You need to write a function that will move it back the right ammount so that its just toutching the wall (or at least make it move back far enough so it no longer thinks its hitting the wall).

    Ali

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    code:

    onClipEvent (enterFrame) {
    speed += 1;
    speed *= .90;
    x = Math.sin(_rotation*(Math.PI/180))*speed;
    y = Math.cos(_rotation*(Math.PI/180))*(speed*-1);
    if (!_root.boundary.hitTest(_x+x, _y+y, true)) {
    _x += x;
    _y += y;
    } else {
    _rotation += 90;
    }
    }


    Your ball bounced back because you were inverting the speed. Also there's no need to position the ball if it hits a wall, unless you are checking for 4 point collision, in this case your not. You'll just end up with errors, because sometimes your checking the wrong direction.

    Hope it helps
    Last edited by mr_malee; 10-17-2005 at 12:22 PM.
    lather yourself up with soap - soap arcade

  4. #4
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    Somthing like this...

    Its not great, you could do it in far better ways than this, but this is a quick solution. I also changed your trig a lil bit, just makes it a bit more like the usual method.

    Ali

    [EDIT]
    or just use that ^^ lol.
    Last edited by alillm; 10-05-2007 at 08:19 AM.

  5. #5
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Mr Malee
    Thanks alot, it works!
    I noticed your changes, not sure what exactly they do ... but I will eventually once Im out of my 'new flash user' stage haha


    Alillm
    [EDIT] Haha I wrote this before yours came up. Anyways thanks alot you two, greatly appreciated

    As to answer your question, as you probably know, Im new to the whole actionscript of Flash so I really dont know the best ways of doing things

    But this method seems to work

    Thanks
    Last edited by Osteel; 10-17-2005 at 12:27 PM.

  6. #6
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    You forgot the .fla . You can check out my .fla if you still want bouncing ^^ mr_malee's way is cleaner imho, but i think mine still works .

    Ali

    [EDIT]
    argh, too many quick posts

  7. #7
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Haha yea

    Well Ill see which best suits me and Ostil's (yes were doing another game together haha) needs, but I really appreciate that you were both willing to help

    Ill be sure to post up the completed one if and when its done

  8. #8
    A Flashkit User
    Join Date
    Sep 2005
    Location
    Ontario, Canada
    Posts
    405
    Thanks guys

    Making games I think is the best way to learn flash =P

    -Ostil-

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