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

2017/9/21

Issues with the increment operator

Beamo Beamo

2017/9/21

#
Hi! I'm relatively new to Greenfoot and the Java programming language in general, and I'm having a few issues with the increment operator (++). Here is an example of what I am trying to do:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class dot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class dot extends Actor
{
    /**
     * Act - do whatever the dot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int dotInterval = 0;
    public void act() 
    {        
        dotInterval++;
        move();
        addDot();
    }    
    private void move(){
        if(Greenfoot.isKeyDown("right")){
            move(4);
        }
        if(Greenfoot.isKeyDown("up")){
            int posY = getY()-4;
            setLocation(getX(),posY);
        }
        if(Greenfoot.isKeyDown("down")){
            int posY = getY()+4;
            setLocation(getX(),posY);   
        }
        if(Greenfoot.isKeyDown("left")){
            move(-4);
        }
    }
    public void addDot(){
        dot dot = new dot();
        if(Greenfoot.isKeyDown("space")){
            while(dotInterval == 2){
                getWorld().addObject(dot,getX(),getY());
                dotInterval++;
            }
        }
    }
}
When I press space however, nothing happens. I'm not entirely sure what I'm doing wrong so if you could please help me out. Any suggestions would be greatly appreciated. Thanks in advance!
danpost danpost

2017/9/21

#
Line 18 is making the value much greater than two, which is required by line 41, long before you get a chance to press the key. Also, it seems pointless to use a while loop at line 41 when it would never do more than one iteration (because if two, it becomes three and it will never loop). As well, it does not make sense to create a dot object before you know one should be created. As such, you are unnecessarily creating a bunch that are never used (one every act cycle). I am not sure what the purpose of the dotInterval field is (as you currently have it used). Maybe you should explain how you want things to operate as far as the field, space key and the creating of dots (which will currently only create one -- with a delay that is pretty much pointless, as is).
Beamo Beamo

2017/9/23

#
Thanks for responding! Basically, I'm trying to create a simple shooter game (not with this code, as it's just an example of the code I'm using to shoot), and I ran into the predicament that whenever I pressed space (the fire button), it shot out a continuous stream of bullets instead of just one. I tried to solve this problem by creating the "bulletInterval" (or in this case the dotInterval) variable, and make it that it would over time increase the integer until it reached the integer "2". This would somewhat interrupt the rate of fire. It seems like a rather odd way to solve the problem but with my knowledge of the java programming language this is all I could think to do. If you have any suggestions as to how I could fix this problem I would greatly appreciate it. :) Thanks!
Super_Hippo Super_Hippo

2017/9/23

#
private int reload = 0, reloadTime = 50;

public void act()
{
    //moving...
    
    if (reload > 0) reload--;
    else if (Greenfoot.isKeyDown("space"))
    {
        Dot dot = new Dot();
        getWorld().addObject(dot, getX(), getY());
        dot.setRotation(getRotation());
        dot.move(2);
        reload = reloadTime;
    }
}
Beamo Beamo

2017/9/23

#
Hi, Super_Hippo! Thanks for the reply, but it's not exactly what I'm looking for as it still results with the long trail of dots. Thanks anyway though! :)
Beamo Beamo

2017/9/23

#
ACTUALLY NEVERMIND! I played around with it a bit and found that what you suggested was the best way to do it, I just had to replace "dot" with "bullet". Thank you! Thanks to everyone who helped me figure this out, it was greatly appreciated.
You need to login to post a reply.