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

2015/6/2

sliding

Jeph Jeph

2015/6/2

#
how would i code an actor to move smoothly eg 10 spaces if a button has been pressed once?
danpost danpost

2015/6/2

#
Add an int step counter field. Set it o 10 when the button is pressed. Only move and decrement the counter when it is not zero.
Muhardanif Muhardanif

2015/6/2

#
please like and vote guys :3 http://www.greenfoot.org/scenarios/14062
Jeph Jeph

2015/6/3

#
thanks i have an idea
Jeph Jeph

2015/6/4

#
@danpost hmm i tried ,but was i even close -_-
int mCount=10; 
    
    public void mcount()
    {
       mCount--; 
    }
    /**
     * Act - do whatever the Player2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        CheckKey();
        if(mCount==6)
        {
            mCount=10;
        }
        
        //replace();
        //turn();
    }  
    public void CheckKey()
    {
        if (Greenfoot.isKeyDown("w")&&mCount<=5)
        {
           setLocation(getX(),getY()-mCount);
           mcount();
        
danpost danpost

2015/6/4

#
As written, if you trace what happens when the act method is called, you end up with this (starting with the value of 'mCount' at '10' via line 1): * the CheckKey method is called (line 13) where the 'if' clause (line 24) will result in a false value as the value of 'mCount' is '10' (not less than or equal to '5') * the 'if' condition on line 14 is checked also resulting in a false value as the value of 'mCount' is still '10' (not equal to '6') As you can see, the value of 'mCount' will never change with what you have there.
danpost wrote...
Add an int step counter field. Set it o 10 when the button is pressed. Only move and decrement the counter when it is not zero.
Things that are not what I suggested: * I said "Set it to 10 when the button is pressed" (not "initially") * I said to both "move and decrement" the counter when it is not zero (not "when it is less than '5'") There are also misunderstandings here. You initially said "if a button has been pressed once". You evidently (1) were referring to a key on the keyboard (not a mouse button); and (2) were referring to the initial down-stroke of the key (not the state of the key being down, which your code suggests). With this new and corrected information, I will suggest that you will also need a boolean field to track the state of the key (whether it was last down or not) so you can determine when the state has changed
Jeph Jeph

2015/6/5

#
@danpost i tired this and the boolean but i know its wrong @_@
int mCount; 
    public void mcount()
    {
        mCount--; 
    }

    /**
     * Act - do whatever the Player2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        CheckKey();
        if(mCount==0)
        {
            mCount=10;
        }

    }  
    public void CheckKey()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            mCount= 10;
            if(mCount>0)
            {
                setLocation(getX(),getY()-mCount);
                mcount();
            }
        }
/**
     * return true if a keyboard key is pressed down
     * else return false
     */
    
    public boolean keyPressed()
    {
        
        if(Greenfoot.isKeyDown("w")||Greenfoot.isKeyDown("s")||Greenfoot.isKeyDown("a")||Greenfoot.isKeyDown("d"))
        return true;
        else 
        return false;
    }
danpost danpost

2015/6/5

#
I said a boolean field -- not a boolean method.
You need to login to post a reply.