Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: A.I. Jumps

  1. #1
    12
    posts
    Registered User

    Fla Script A.I. Jumps

    I looked but did not find it so if this question is already asked please redirect me.

    Hi im new here so hi....

    Question:
    Working in Flash CS3 with AS 3, I'm trying to make a character Jump as soon it detects there is no "platform" or there is a higher "platform" in the next few pixels.

    The "character" walks in the direction of the mouse, but i want it to jump on its own. I know i can add a Keyboard event but that will destroy the whole purpose of my idea.

    So if any one has a clue ...its all welcome.

    also im using this code for the platform collision( AND IT SUCKS =( )

    private function Collide() {
    y+= _YV
    ;
    while (_PLATFORM.hitTestPoint(x,y-1,true)) {
    y-=1 ;
    }
    while (_PLATFORM.hitTestPoint(x+width/2,y-1,true)) {
    x-=1;
    }
    while (_PLATFORM.hitTestPoint(x-width/2,y-1,true)) {
    x+=1 ;
    }

    (Im working with OOP)

    Thanks in advance,

    Sidar

  2. #2
    Hi Sidar,

    This is just the way I'd do it, it might not be the best way in the world but I would imagine it would work...

    (i never know how to indent my code on these forums so apologies for it being untidy)

    Code:
    private function Collide() {
    y+= _YV
    ;
    while (_PLATFORM.hitTestPoint(x,y-1,true)) {
    y-=1 ;
    }
    while (_PLATFORM.hitTestPoint(x+width/2,y-1,true)) {
    x-=1;
    }
    while (_PLATFORM.hitTestPoint(x-width/2,y-1,true)) {
    x+=1 ;
    }
    
    // if we're going rightwards, test the point ahead of us to see if we need to jump..
    if(_XV>0)
    {
    if(!_PLATFORM.hitTestPoint(x+1,y-1,true))
    {
    makeThePlayerJumpHere(JumpRightwards);
    return;
    }
    }
    else
    // Are we going leftwards?
    if(_XV<0)
    {
    if(!_PLATFORM.hitTestPoint(x-1,y-1,true))
    {
    makeThePlayerJumpHere(JumpLeftwards);
    return;
    }
    }

  3. #3
    12
    posts
    Registered User
    Thanks for the reply,

    But now my MC's are just floating, I tried using a jump function between each while loop but then it just keeps on jump the moment it hits a wall( and eventually jumps out of screen)

    Dont you know a website or something solving this?
    ( i looked at the website explaining how to make tiled based games but the collision code...i really did not get it and i'm not working with tiles)
    or does any one else have a clue?
    Last edited by Sidar; July 17th, 2008 at 07:46 AM.

  4. #4
    hmmm, I think I'd need to see your game to understand whats happening... Is there anyway you can upload it anywhere?

    oh and I'm not sure of any tutorials anywhere that deal with this exact problem but someone else might

  5. #5
    Quote Originally Posted by andrew2110 View Post
    Code:
    if(!_PLATFORM.hitTestPoint(x-1,y-1,true))
    {
    makeThePlayerJumpHere(JumpLeftwards);
    return;
    }
    you are checking if you are NOT hitting that point so most likely you would be always be calling that jump function

  6. #6
    Sidar, theres lots of ways to do what you want, but until and unless i see something of your game, i cant say much.But i can say;

    You can do this either in the way of code....means lots of thinking for you(Just check if he is on ground and the x+speed,y+2 position is collision free or not, before jumping)
    Or you can take the easy road, place invisible marker objects along the way, and when he collides with such a block, he jumps....simple!!
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  7. #7
    make a small simbol almost invisible a lil bit in front of your char
    (a few pixels ahead in the direction it's facing)

    then if the small simbol isn't colliding you subtract <you jump strength here> from the vertical velocity
    (i hope you are using a vertical velocity to make gravity)

  8. #8
    12
    posts
    Registered User
    @bluemagica,
    I really do not want to take the easy way out...it feels like if there is no effort put in it XD.

    I'm actually having a bit of a problem understanding this whole collision thing, so im still trying to find out how it exactly works.

    edit: I'm trying to understand the part about x+speed,y+2 , so its going to be like in my case _platform.hitTestPoint(x+_XV,y+2)....but how does this detect pixels ahead?

    @Spark_BR,
    Since im working with Classes i want it to be as optimized. I rather not have a new parameter/argument that uses an external MC to detect gaps or higher platforms.

    Speaking of higher platforms, i only see that you people try to help me( and im glad you are) with gaps in platforms only...but what about higher platforms?...how should a object detect that? How will it detect if the higher platform can be reached to begin with or not.

    well here is a test, only with my code before the help.

    http://img373.imageshack.us/my.php?image=mtestji3.swf

    as you can see it nicely detects walls/platforms. But the moment they fall into the gap they will fall down....as they should ( i didnt change the code for this one ). I also notice that they are getting pushed a bit to the direction they are walking in. I guess thats because the MC detects a right or left collision.

    So what do you people think?

    thnx for all the reply's,

    Sidar

    edit:
    Okai i i guess i made a mistake with gravity, they do jump now but its pretty much choppy. Does anyone know to fix this?

    http://img140.imageshack.us/my.php?image=test2rq6.swf

    also the MC's are now in the middle of the platform...?
    And i want them to jump before they actually hit the empty space >_<

    Attached AS.
    Attached Files Attached Files
    Last edited by Sidar; July 17th, 2008 at 02:55 PM.

  9. #9
    make them jump only if some property like "isInAir" is false and the vertical speed = 0
    i saw some of them jumping twice

    off:
    about the lil mc i was kidding by telling you to do that way
    i would suggest a createEmptyMovieClip("sensor", "sensor"); but it's the same...

  10. #10
    12
    posts
    Registered User
    They jump twice simply because it " detects" that edge again. Not because its in mid-air.( you only see them jump near a edge)

  11. #11
    Quote Originally Posted by yaim0310 View Post
    you are checking if you are NOT hitting that point so most likely you would be always be calling that jump function
    I thought that was the point? If the sprite is not going to be hitting a platform 1 place ahead of where it is now, it will have to jump? Have i misunderstood?

  12. #12
    Quote Originally Posted by andrew2110 View Post
    I thought that was the point? If the sprite is not going to be hitting a platform 1 place ahead of where it is now, it will have to jump? Have i misunderstood?
    but what that was doing was making you constantly jumping no matter where you are on the map except when it is 1 place ahead of it. the code said to jump when it is NOT 1 place ahead of you.

  13. #13
    I'm still confused..

    what I thought my code would do is 'if the point ahead of where I'm going is not on a platform, do a jump'. It's not doing 'if this sprite is colliding with the point ahead of where it currently is, jump', as you say that would always lead to it jumping

    I would (as suggested) have either an inAir flag or 'onGround' flag. And on your games main loop for each sprite have something like:

    'if inAir
    applyGravity();

    Collide();'


    I'm trying to understand the part about x+speed,y+2 , so its going to be like in my case _platform.hitTestPoint(x+_XV,y+2)....but how does this detect pixels ahead?
    This will detect pixels ahead as (and I'm making assumptions about x being the current x position of the sprite,XV being the the current X velocity and _platform being a movie clip containing all the platform images or rectangles that make up a level) it will test whether the point at x+_XV is still going to be hitting the platform. So for example, given values of x=50, _XV = 2. It will check at co-ordinates (52,y+2) for a collision with the platform object.... I imagine y+2 is there because each platform is >2 pixels wide and having just y there alone would cause your sprites to never really touch the surface...

    Or this is my understanding anyway.

  14. #14
    I always use something like:
    Code:
    if(not inAir and _YV == 0){
        <your hitTest here>{
            this.jump();
        }
    }
    
    if(_YV > _root._gravityStrenght){
        this.inAir = true;
        <play fall anim>
    }
    
    if(_YV == 0){
        this.inAir = false;
        <stop fall anim>
    }
    Code:
    function jump(){
        this._YV -= this._jumpStrenght;
        this.inAir = true;
        <play jump anim>
    }
    note: the gravity strenght must not be a round number, i always use 1.2 for example

    edit: that's how i make the player kick the walls, dividing _YV until it's smaller than the gravity acel.: then he jumps twice;
    Last edited by SparK_BR; July 18th, 2008 at 08:22 AM.

    "A human can interpret missing logic but a computer can't." - wkt
    "That goes into the 'too bad' box." - TOdorus
    "This is a three year old thread, there's no reason to post here..." - flyingmonkey456
    "Despite beeing the oposite, they look very similar to me." - SparK

  15. #15
    12
    posts
    Registered User
    @andrew,
    Thnx

    @Spark....
    I'm really trying to work this code into my class but i get lost pretty quick.
    IF you look in my class i used a "Jumping" boolean....should i replace that with "inAir"?...

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012