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:
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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++; } } } } |