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

2018/6/28

How to use Wait

Ticker175 Ticker175

2018/6/28

#
hey guys I'm having trouble with making my actor/player stop directly at a wall and wait until it is moved) can only move on the x or y axis the problem is when the player is touching a wall it bounces and i dont want it to
danpost danpost

2018/6/28

#
Ticker175 wrote...
when the player is touching a wall it bounces and i dont want it to
Show the class codes for the player (use code tags).
Ticker175 Ticker175

2018/6/28

#
public class Pacman extends Actor
{
    private int timer;
    public int mCounter = 0;
    public int mChange = 10;
    public int qCounter = -10;
    public int Death = 0;
    public int life = 1;
    public int stop = 0;
    GreenfootSound Respawn = new GreenfootSound("Respawn.wav");
    GreenfootSound Fail = new GreenfootSound("Fail Sound.mp3");
    
    public void act() 
    {
        //Pacman_Hitbox();
[b]        movement();[/b]
        Animation();
        DeathAnimation();
[b]        NoClipOff();
        if (Death == 0){
            move(2);
        }[/b]
    }
    
    //Mouth Animation
    private void Animation() 
    {
        if (Death == 0){
            mCounter++;
            if (mCounter >= mChange)
                {
                    setImage("Pacman1.png");
                    
                    if(mCounter == 2*mChange) mCounter = 0;  
                }
                
            else  setImage("Pacman2.png");
        }
    }
    //Death Animation
    private void DeathAnimation() 
    {
        if (life==0){
            Death = 1;
            qCounter++;
            if (qCounter == 0){
                setImage("PacmanDeath1.png");
            }
            else if (qCounter == 10){
                setImage("PacmanDeath2.png");
            }
            else if (qCounter == 15){
                setImage("PacmanDeath3.png");
            }
            else if (qCounter == 20){
                setImage("PacmanDeath4.png");
            }
            else if (qCounter == 25){
                setImage("PacmanDeath5.png");
            }
            else if (qCounter == 30){
                setImage("PacmanDeath6.png");
            }
            else if (qCounter == 35){
                setImage("PacmanDeath7.png");
            }
            else if (qCounter == 40){
                setImage("PacmanDeath8.png");
            }
            else if (qCounter == 45){
                setImage("PacmanDeath9.png");
            }
            else if (qCounter == 50){
                setImage("PacmanDeath10.png");
            }
            else if (qCounter == 55){
                setImage("PacmanDeath11.png");
            }
            else if (qCounter == 60){
                setImage("PacmanDeath12.png");
            }
            else if (qCounter == 100){
                setImage("Bounds.png");
                Greenfoot.setWorld(new PacWorld());
            }
        }
    }
    
    
[b]    public void movement()
    {
        if (isTouching(Wall.class))
        {
            if (Greenfoot.isKeyDown("w"))
            {
                if (isTouching(Intercection.class)){
                    setLocation(getX()+2, getY());
                    setRotation(270);
                }
            }
            if (Greenfoot.isKeyDown("a"))
            {
                if (isTouching(Intercection.class)){
                    setLocation(getX(), getY());
                    setRotation(180);
                }
            }
            if (Greenfoot.isKeyDown("s"))
            {
                if (isTouching(Intercection.class)){
                    setLocation(getX()-2, getY());
                    setRotation(90);
                }
            }
            if (Greenfoot.isKeyDown("d"))
            {
                if (isTouching(Intercection.class)){
                    setLocation(getX(), getY()-2);
                    setRotation(0);
                }
            }
        }
    }
    
    public void NoClipOff()
    {
        if (isTouching(Wall.class))
        {
            getIntersectingObjects(Wall.class);
            //move(0);
            //setLocation(getX()-2, getY());
            setLocation(getX()-2, getY());
            //wait(stop );
        }
    }[/b]
}
Ticker175 Ticker175

2018/6/28

#
In Act Class movement(); Animation(); DeathAnimation(); NoClipOff(); if (Death == 0){ move(2); }
Ticker175 Ticker175

2018/6/28

#
In movement and noclipoff (the one that stops it from going through the wall) class public void movement() { if (isTouching(Wall.class)) { if (Greenfoot.isKeyDown("w")) { if (isTouching(Intercection.class)){ setLocation(getX()+2, getY()); setRotation(270); } } if (Greenfoot.isKeyDown("a")) { if (isTouching(Intercection.class)){ setLocation(getX(), getY()); setRotation(180); } } if (Greenfoot.isKeyDown("s")) { if (isTouching(Intercection.class)){ setLocation(getX()-2, getY()); setRotation(90); } } if (Greenfoot.isKeyDown("d")) { if (isTouching(Intercection.class)){ setLocation(getX(), getY()-2); setRotation(0); } } } } public void NoClipOff() { if (isTouching(Wall.class)) { getIntersectingObjects(Wall.class); //move(0); //setLocation(getX()-2, getY()); setLocation(getX()-2, getY()); //wait(stop ); } } }
danpost danpost

2018/6/28

#
Ticker175 wrote...
How to use Wait
Let me explain a couple of things: (1) move(0) is a nothing statement; it says have the actor NOT move from where it currently is, which is exactly what it will do without telling it to; it does not negate any previous move commands that have already been executed; (2) wait has nothing to do with where the actor is; it is a thread command which you, as a non-system programmer, should not use; (3) setLocation(getX()-2, getY()) will only revert movement done after setLocation(getX()+2, getY()) is executed, provided that the actor has not changed rotation since then. The way to "stop" an actor is to always allow it to move when directed, but then revert the move when a wall is encountered. Since the rotation of the actor is intended to be facing the direction the actor is moving, best is to use setRotation first, then use move(2) to move. That way, when a wall is encountered, you can use move(-2) to "stay" off the wall (until directed to turn and move and new direction).
Ticker175 Ticker175

2018/6/29

#
Hey dan Thanke for the response 1.) yeah i know i was just trying everything i could think off i had it commented and have deleted i 2.) ok thanks i thought that it paused the actors actions until a condition becomes true/false 3.) yeah thats the problem that i was having and i couldnt think of anything else to do so i aksed the fourms ok that sounds awesome but i have one question about the function of "reverting" the movement with (-2). wont that create the same effect as i already have with the bouncing??? you have given me an idea though which i will probably try first: if i make the actor move(2) and when they encounter a wall move(-2) ONCE then stop moving, by setting an "if" command that encloses the move() to false
danpost danpost

2018/6/29

#
Ticker175 wrote...
if i make the actor move(2) and when they encounter a wall move(-2) ONCE then stop moving, by setting an "if" command that encloses the move() to false
You were fine up to the then. You do not need any extra conditions. As long as move(2) and move(-2) are done during the same act cycle (again, provided the rotation is not changed between the two calls), the actor will appear to be stopped (no intermediate placement will be evident to the viewer; only the final placement at the end of the act will be seen). Try a simple act method like the following for testing purposes:
public void act()
{
    move(2);
    if (isTouching(Wall.class)) move(-2);
}
Ticker175 Ticker175

2018/6/29

#
Hey Dan thanks for the speedy response ill try that does greenfoot run on game-ticks and/or act cycles i have a general question. i was wondering how many ticks there are in a second in greenfoot (im guessing that greenfoot uses ticks like in most games) Thanks for the help
Ticker175 Ticker175

2018/6/29

#
Thanks for all the help its working now =) here is the code i changed
    public void act() 
    {
        //Pacman_Hitbox();
        movement();
        Animation();
        DeathAnimation();
        NoClipOff();
        if (Death == 0){
            move(2);
            if (isTouching(Wall.class)) move(-2);
        }
    }
i also deleted the whole "noclipoff" part of the code =)
danpost danpost

2018/6/29

#
Ticker175 wrote...
does greenfoot run on game-ticks and/or act cycles i have a general question. i was wondering how many ticks there are in a second in greenfoot (im guessing that greenfoot uses ticks like in most games
Greenfoot does have game ticks. It is normally governed by the scenario speed (1 to 100). In most standard size projects where the speed is not excessively high, an act cycle will be executed between game ticks with time to spare. A scenario will run about 60 act cycles per second at normal running speed (with speed slider at the mid-point (speed value of 50). The speed can be altered by using the Greenfoot.setSpeed(int) method (any place in your codes) or by manual adjusting the slider (which will alter the initial speed of your scenario, generally speaking). In 99.9% of scenarios, a speed between 30 and 60 is sufficient. There are situations, most that can be avoided, where lagging may occur due to poor programming. Lag happens when the execution of an act cycles takes longer than the time between game ticks.
Ticker175 Ticker175

2018/7/2

#
Ok cool thanks =)
You need to login to post a reply.