i stored a object into a list but how do I add that object from the list to the world?
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 48 49 50 51 52 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ArrayList; /** * Write a description of class body here. * * @author (your name) * @version (a version number or a date) */ public class player extends Actor { //player info int p_rotation = 0 ; int p_turnspeed = 3 ; int p_speed = 3 ; int p_weapon = 0 ; static List <Object> item = new ArrayList<Object>(); static int p_skin = 1 ; /** * Act - do whatever the player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. getWorld().showText( "" +item.size(), 100 , 100 ); playermovement(); } void playermovement() { if (Greenfoot.isKeyDown( "D" )) { p_rotation = p_rotation+p_turnspeed; setRotation(p_rotation); } if (Greenfoot.isKeyDown( "A" )) { p_rotation = p_rotation-p_turnspeed; setRotation(p_rotation); } if (Greenfoot.isKeyDown( "W" )) { move(p_speed); } if (Greenfoot.isKeyDown( "S" )) { move(-p_speed+ 2 ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class item here. * * @author (your name) * @version (a version number or a date) */ public class item extends Actor { /** * Act - do whatever the item wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (isTouching(player. class )) { getWorld().getObjects(player. class ).get( 0 ).item.add( this ); getWorld().removeObject( this ); } } } |