How can I make it so that a specific sequence of button inputs would result in an action? For example, pressing p, then i, then e to make some pie appear.
String builder = ""; public void act() { String key = Greenfoot.getKey(); if (key != null && key.length() == 1) { builder = builder + key; if (builder.endsWith("pie")) { getWorld().addObject(new Pie(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); builder = ""; // once a string is found, we can clear builder } if (builder.endsWith( // etc. // rest of act }
builder = builder + key.toLowerCase();
private int speed = 7; private int vSpeed = 1; private int acceleration = 1; String builder = ""; public void act() { checkKeys(); checkFall(); getattacked(); String key = Greenfoot.getKey(); if (key != null && key.length() == 1) { builder = builder + key.toLowerCase(); if (builder.endsWith("hot")) { setImage("pikachuduckpro.png"); builder = ""; } } }
setImage(new GreenfootImage("pikachuduckpro.png"));
public class Pikachu extends Actor { /** * Act - do whatever the Pikachu wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int speed = 7; private int vSpeed = 1; private int acceleration = 1; String builder = ""; public void act() { checkKeys(); checkFall(); getattacked(); String key = Greenfoot.getKey(); if (key != null && key.length() == 1) { builder = builder + key.toLowerCase(); if (builder.endsWith("hot")) { setImage(new GreenfootImage("pikachuduckpro.png")); builder = ""; } } } private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { setImage("pikafightleftpro2.png"); moveLeft(); } if (Greenfoot.isKeyDown("right") ) { setImage("pikfightrightpro.png"); moveRight(); } if (Greenfoot.isKeyDown("up") ) { setImage("pikajumppro.png"); moveUp(); } if (Greenfoot.isKeyDown("down") ) { setImage("pikachuduckpro.png"); } if ("shift".equals(Greenfoot.getKey())) { thunderbolt(); } } private void thunderbolt() { Thunderbolt thunderbolt = new Thunderbolt(); getWorld().addObject(thunderbolt, getX(), getY()); thunderbolt.setRotation(Greenfoot.getRandomNumber(360)); } private void thunder() { Thunder thunder = new Thunder(); getWorld().addObject(thunder, getX(), getY() - 100); } public boolean onGround() { Actor under = getOneObjectAtOffset(0, 50, Ground.class); return under != null; } public void checkFall() { if(onGround() ) vSpeed= 0; else { fall(); } } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void moveRight() { setLocation ( getX() + speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); } public void moveUp() { setLocation ( getX(), getY() - speed * 2); } private void getattacked() { Actor Ember; Ember = getOneObjectAtOffset(0, 0, Ember.class); if (Ember !=null) { bg1 world; world = (bg1) getWorld(); world.removeObject(Ember); Greenfoot.playSound("burn.wav"); world.bar.subtract(3); } Actor Ember2; Ember2 = getOneObjectAtOffset(0, 0, Emberl.class); if (Ember2 !=null) { bg1 world; world = (bg1) getWorld(); world.removeObject(Ember2); Greenfoot.playSound("burn.wav"); world.bar.subtract(3); } } }
if ("space".equals(key))