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

2023/2/13

Making an attack delay

Shaormentiul Shaormentiul

2023/2/13

#
I am struggling on finding a way to delay an attack in my game. I have a character that is moving, and I want it to stop moving for a few moments before it can move again. I tried googling solutions but i cant seem to find a way to do this.I think its because of the way I arranged the code, because im really new to Greenfoot. How can I add this delay?
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class Person extends Actor
{
    private boolean isAttacking = false;
    
    

    /**
     * 
     */
    public Person()
    {
        GreenfootImage image = getImage();
        image.scale(500, 500);
        setImage(image);
        
        
        
    }

    /**
     * Act - do whatever the Person wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    final public void act()
    {
        int health = 100;
        int damage = 5;
        int power = 0;
        long lastAdded = System.currentTimeMillis();
        if (Greenfoot.isKeyDown("right") &&  ! isAttacking) {
            setLocation(getX() + 5, getY());
        }
        if (Greenfoot.isKeyDown("left") &&  ! isAttacking) {
            setLocation(getX() - 5, getY());
            
        }
        long curTime = System.currentTimeMillis();
        if ("x".equals(Greenfoot.getKey())) {
            isAttacking = true;
            atac();
            Greenfoot.delay(5);
        }
    }

    /**
     * 
     */
    public void atac()
    {
        Attack attack =  new  Attack();
        getWorld().addObject(attack, getX() + 50, getY());
    }
}
danpost danpost

2023/2/14

#
Shaormentiul wrote...
I am struggling on finding a way to delay an attack in my game. I have a character that is moving, and I want it to stop moving for a few moments before it can move again. I tried googling solutions but i cant seem to find a way to do this.I think its because of the way I arranged the code, because im really new to Greenfoot. How can I add this delay? << Code Omitted >>
Do not use Greenfoot.delay unless you are delaying the entire scenario for some time. You only want to delay an actor. So, instead of using a boolean, use and int counter to track the number of act steps that the actor is to pause for. When its value is zero, no delay is happening. When its value is not zero, do not have any code, other than that checks to see if it is zero, run in the act method. Also, your health, damage and power fields are misplaced. Where they are, they will be reset to the values given on those lines every act step, giving the appearance of never changing at all. Try this code:
import greenfoot.*;

public class Person extends Actor
{
    private int health = 100;
    private int damage = 5;
    private int power = 0;
    private int attackDelay = 0;
    
    public Person()
    {
        GreenfootImage image = getImage();
        image.scale(500, 500);
        setImage(image);
    }
    
    public void act()
    {
        if (attackDelay > 0 && --attackDelay > 0) return; 
        int dx = 0;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (Greenfoot.isKeyDown("left")) dx--;
        setLocation(getX()+5*dx, getY());
        if (Greenfoot.isKeyDown("x")) atac()
    }
    
    private void atac()
    {
        attackDelay = 200; // adjust as needed
        getWorld().addObject(new Attack(), getX()+50, getY());
    }
}
Q: Is there a reason for the image of your Person object to be so huge?
Shaormentiul Shaormentiul

2023/2/14

#
A: Yes, I want to create a Street FIghter-type game So I copied the code and it doesnt work as intended, after I attack the character cant move or attack anymore at all, the game basically freezes. I also dont know how to use the -- and ++ operators. I assume they work the same way they do in c++.I tried lowering the value of attack delay to 1 but it didnt help. How can I make it work?
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class Person extends Actor
{
    private int health = 100;
    private int damage = 5;
    private int power = 0;
    private int attackDelay = 0;
    
    

    /**
     * 
     */
    public Person()
    {
        GreenfootImage image = getImage();
        image.scale(500, 500);
        setImage(image);
        
        
        
    }

    /**
     * Act - do whatever the Person wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    final public void act()
    {
        if (attackDelay > 0 &&  -  - attackDelay > 0) {
            return;
        }
        int dx = 0;
        if (Greenfoot.isKeyDown("right")) {
            dx = dx + 1;
        }
        if (Greenfoot.isKeyDown("left")) {
            dx = dx - 1;
            
        }
        setLocation(getX() + 5 * dx, getY());
        if ("x".equals(Greenfoot.getKey())) {
            if ( ! Greenfoot.isKeyDown("down")) {
                atac();
            }
            if (Greenfoot.isKeyDown("down")) {
                atacDown();
            }
        }
    }

    /**
     * 
     */
    private void atac()
    {
        attackDelay = 1;
        getWorld().addObject( new  Attack(), getX() + 50, getY());
    }

    /**
     * 
     */
    private void atacDown()
    {
        AttackD attackJ =  new  AttackD();
        getWorld().addObject(attackJ, getX() + 50, getY() + 50);
    }
}
danpost danpost

2023/2/14

#
Shaormentiul wrote...
A: Yes, I want to create a Street FIghter-type game
Still, seems huge.
So I copied the code and it doesnt work as intended, after I attack the character cant move or attack anymore at all, the game basically freezes. I also dont know how to use the -- and ++ operators. I assume they work the same way they do in c++.I tried lowering the value of attack delay to 1 but it didnt help. How can I make it work? << Code Omitted >>
The two characters making up the symbols for incrementing or decrementing the value of a variable must not have any white space between them. The operators are "++" and "--" (not "+ +", "+ +", etc. or "- -", "- -", etc.). Since the value of the field is not changing as you coded it, the delay remains in effect indefinitely. See the bottom section of the java tutorial page here, titled The Unary Operators. Note that as you coded it, the compiler thinks you are double negating the value, which has no effect on it.
Shaormentiul Shaormentiul

2023/2/14

#
I cant erase the space between them, it just deletes the left one. I tried copy and pasting your code
Shaormentiul Shaormentiul

2023/2/14

#
Nevermind, I made it work. Thanks for the help a lot!
You need to login to post a reply.