This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alwin_Gerrits
Alwin_Gerrits wrote ...

2014/10/12

Need help :)

Alwin_Gerrits Alwin_Gerrits

2014/10/12

#
Hey guys, I'm kinda new to Greenfoot and I'm running into a problem that seems to appear without a cause. I tried finding clues as to where the problem resides within my code and I'm pretty sure the problem is located within this part of my code (see beneath). Just to make it clear, I'm trying to make a small platforming game and the problem is that the code somehow constantly picks the getOneObjectAtOffset rule within the code as true. Even though it's scanning for the class platform and I walked right of the platform like 10 seconds before. So now my character just floats in midair. I have no problem placing my complete code here but it's about 160 rows long so I would prefer not having to bother you guys with it.
Alwin_Gerrits Alwin_Gerrits

2014/10/12

#
public void checkPlatform()
{
       if (getOneObjectAtOffset(0, 25, platform.class)!=null) 
       {                                                                                    
           SelectNiveau();
           fallingSpeed=0;
           resetJump();
       }
              
       else if (getY()>=bottomLevel-10) 
       {
           setLocation(getX(), bottomLevel); 
           fallingSpeed = 0;
           resetJump();
       }
       
       else if (getY()<bottomLevel)
       {           
           if (jumpSpeed==0)
           {fall();}
       }
    }
danpost danpost

2014/10/12

#
I think the problem is that you only 'fall' when the value of 'jumpSpeed' is zero (see lines 19 and 20), which seems like saying 'if your vertical speed is zero, then fall' (which would not do anything).
Alwin_Gerrits Alwin_Gerrits

2014/10/13

#
actually my jumpspeed is literally the speed the player has in an upward direction. When the player is on an upwards speed of 0, then fall should react. Seen fall starts of with a speed of 1 I don't see how this could form the problem, but I will definitely look into it. Thanks for now. I will try what happens when changing it to react to jumpspeed=-1 along with some other solutions
Alwin_Gerrits Alwin_Gerrits

2014/10/13

#
Hey, ty for the help. The problem was that if resetJump gets called it resets jumpSpeed to maxSpeed instead of 0. So after hitting a platform it indeed should never be able to say jumpSpeed==0 because it gets reset to maxSpeed. Again thanks for the fast reply and you were deadon with the solution. A simple else if(jumpSpeed==maxSpeed) {fall();} fixed the entire problem.
danpost danpost

2014/10/13

#
The field 'jumpSpeed' is usually used as a constant value for what to add to the vertical speed field when a jump is initiated. 'maxSpeed' (also usually used as a constant value) may be used to limit the actual downward speed when falling. I do not see how the two field would in any way be linked (or used together -- like 'jumpSpeed == maxSpeed').
Alwin_Gerrits Alwin_Gerrits

2014/10/16

#
'JumpSpeed' is used as a counter to determine how fast the player jumps when 'jump()' gets called. By setting 'jumpSpeed' to 'maxSpeed' then substaction 1 with each time the program runs through the code the player gets raised less and less untill 'jumpSpeed' equals 0. At that point 'fall()' gets called on. Seen I want the player to fall at a maximum velocity that equals the speed of the moment he jumps I simply set the 'maxSpeed' as value of an if-statement. So if( fallSpeed!=maxSpeed) then fallSpeed++ and if(fallSpeed=maxSpeed) ... well then it just keeps falling at the maximum speed. So really fall() simply is a followup for jump() which gets activated when 'jumpSpeed' is either 0 or equal to maxSpeed //which I use in case he walks of a platform. So he can only fall() if he reaches the top of his jump, hits a platform during his jump or walks of a platform. Just to make myself a little clearer, i used 'jumpSpeed' as a non-constant counter to determine his upward speed. 'maxSpeed' is indeed a constant value which is used to limit his speed when falling, but I allso use it to determine how high the jump can be. Again the faster his starting speed is the higher he jumps because ... well actually I would like some help with this now that I'm responding anyway. I made the code from 'jumpSpeed' a bit different so it would scan for a platform above with every time the player gets raised by 1. Yet somehow it doesn't seem to be reacting properly. Like it isn't scanning every time or doesn't stop if it found a platform. I actually found that on some occasions the players head will go partially into the platform while jumping.. Now this isn't much of a problem, but my function to move gets bugged thanks to it. If I can find a way to prevent my player from getting into the block even a bit that would fix it though. Any thoughts?
public void jump()
    {
        Actor platformAbove = getOneObjectAtOffset(0, -20, platform.class);
        
        if (Greenfoot.isKeyDown("up")) 
        {jumped = true;}
        
        if ((jumped) && (jumpSpeed!=0)) //if "up" is pressed down and player isn't at the top of his jump
        {
            int counter;
            fallingSpeed = 0;

            for (counter=jumpSpeed*2; counter>0; counter--)
            {
                setLocation(getX(), getY()-1);
                
                if (platformAbove!=null) //if there's a platform above the player
                {
                    jumpSpeed=0;
                    counter=0;
                }
            }
            
            if(!(platformAbove!=null))
            {jumpSpeed--;} //shouldn't be called if there is a platform because it would make jumpSpeed -1
        }
    }
Alwin_Gerrits Alwin_Gerrits

2014/10/17

#
Nvm about the new problem, I fixed it myself :). Turns out it doensn't change it's position per 1 then checks, but instead changes it's position jumpSpeed*2 times in an instant and then scans... even though I don't know the cause I allready fixed simular problem in the fall() code, so when I found the problem to be the same the solution was found prety fast :). Ty anyway :).
You need to login to post a reply.