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

2015/1/15

Need Help ASAP

n1nikko n1nikko

2015/1/15

#
How do i call a method from a different class //PlayerClass import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Erm { private GreenfootImage image1; // Sprite for first right character movement private GreenfootImage image2; // Sprite for second right character movement private GreenfootImage image3; // Sprite for third right character movement private GreenfootImage image4; // Sprite for fourth right character movement private GreenfootImage image5; // Sprite for fifth right character movement private GreenfootImage image6; // Sprite for sixth right character movement private GreenfootImage image7;; // Sprite for seventh right character movement private GreenfootImage image8; // Sprite for eighth right character movement private GreenfootImage image9; // Sprite for ninth right character movement private GreenfootImage image10; // Sprite for tenth right character movement private GreenfootImage image11; // Sprite for tenth right character movement private GreenfootImage image12; // Sprite for tenth right character movement private GreenfootImage image13; // Sprite for tenth right character movement private GreenfootImage image14; // Sprite for tenth right character movement private GreenfootImage image15; // Sprite for tenth right character movement private GreenfootImage image16; // Sprite for tenth right character movement private GreenfootImage image1L; // Sprite for first right character movement private GreenfootImage image2L; // Sprite for second right character movement private GreenfootImage image3L; // Sprite for third right character movement private GreenfootImage image4L; // Sprite for fourth right character movement private GreenfootImage image5L; // Sprite for fifth right character movement private GreenfootImage image6L; // Sprite for sixth right character movement private GreenfootImage image7L;; // Sprite for seventh right character movement private GreenfootImage image8L; // Sprite for eighth right character movement private GreenfootImage image9L; // Sprite for ninth right character movement private GreenfootImage image10L; // Sprite for tenth right character movement private GreenfootImage image11L; // Sprite for tenth right character movement private GreenfootImage image12L; // Sprite for tenth right character movement private GreenfootImage image13L; // Sprite for tenth right character movement private GreenfootImage image14L; // Sprite for tenth right character movement private GreenfootImage image15L; // Sprite for tenth right character movement private GreenfootImage image16L; // Sprite for tenth right character movement private int vSpeed =4; private int acceleration = 2; private boolean jumping; private int jumpStrength = 20; private int speed = 4; public Player() { image1 = new GreenfootImage ("sprite1.png"); // image2 variable allocates Character2.png to generate sprite image2 = new GreenfootImage ("sprite2.png"); // image3 variable allocates Character3.png to generate sprite image3 = new GreenfootImage ("sprite3.png"); // image4 variable allocates Character4.png to generate sprite image4 = new GreenfootImage ("sprite4.png"); // image5 variable allocates Character5.png to generate sprite image5 = new GreenfootImage ("sprite5.png"); // image6 variable allocates Character6.png to generate sprite image6 = new GreenfootImage ("sprite6.png"); // image7 variable allocates Character7.png to generate sprite image7 = new GreenfootImage ("sprite7.png"); // image8 variable allocates Character8.png to generate sprite image8 = new GreenfootImage ("sprite8.png"); // image9 variable allocates Character9.png to generate sprite image9 = new GreenfootImage ("sprite9.png"); // image10 variable allocates Character10.png to generate sprite image10 = new GreenfootImage("sprite10.png"); // image11 variable allocates Character11.png to generate sprite image11 = new GreenfootImage("sprite11.png"); // image11 variable allocates Character11.png to generate sprite image12 = new GreenfootImage("sprite12.png"); // image11 variable allocates Character11.png to generate sprite image13 = new GreenfootImage("sprite13.png"); // image11 variable allocates Character11.png to generate sprite image14 = new GreenfootImage("sprite14.png"); // image11 variable allocates Character11.png to generate sprite image15 = new GreenfootImage("sprite15.png"); // image11 variable allocates Character11.png to generate sprite image16 = new GreenfootImage("sprite16.png"); // image11 variable allocates Character11.png to generate sprite image1L = new GreenfootImage ("sprite1L.png"); // image2 variable allocates Character2.png to generate sprite image2L = new GreenfootImage ("sprite2L.png"); // image3 variable allocates Character3.png to generate sprite image3L = new GreenfootImage ("sprite3L.png"); // image4 variable allocates Character4.png to generate sprite image4L = new GreenfootImage ("sprite4L.png"); // image5 variable allocates Character5.png to generate sprite image5L = new GreenfootImage ("sprite5L.png"); // image6 variable allocates Character6.png to generate sprite image6L = new GreenfootImage ("sprite6L.png"); // image7 variable allocates Character7.png to generate sprite image7L= new GreenfootImage ("sprite7L.png"); // image8 variable allocates Character8.png to generate sprite image8L = new GreenfootImage ("sprite8L.png"); // image9 variable allocates Character9.png to generate sprite image9L = new GreenfootImage ("sprite9L.png"); // image10 variable allocates Character10.png to generate sprite image10L = new GreenfootImage("sprite10L.png"); // image11 variable allocates Character11.png to generate sprite image11L = new GreenfootImage("sprite11L.png"); // image11 variable allocates Character11.png to generate sprite image12L = new GreenfootImage("sprite12L.png"); // image11 variable allocates Character11.png to generate sprite image13L = new GreenfootImage("sprite13L.png"); // image11 variable allocates Character11.png to generate sprite image14L = new GreenfootImage("sprite14L.png"); // image11 variable allocates Character11.png to generate sprite image15L = new GreenfootImage("sprite15L.png"); // image11 variable allocates Character11.png to generate sprite image16L = new GreenfootImage("sprite16L.png"); // image11 variable allocates Character11.png to generate sprite setImage(image1); } /** * 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() { Actor GoldCoin = getOneIntersectingObject(GoldCoin.class); // Actor class allocates Coin subclass to be used as a removable object and a method of points collection if (GoldCoin!=null) // Method used to remove coin object when Player 1 or Player 2 walks or jumps on coin { World myWorld = getWorld(); myWorld.removeObject(GoldCoin); Background background = (Background)myWorld; GoldCounter counter = background.getGoldCounter(); counter.addPoints(); GrandTotal GrandTotal = background.getGrandTotal(); GrandTotal.addGoldPoints(); } Actor RedCoin = getOneIntersectingObject(RedCoin.class); if (RedCoin!=null) // Method used to remove coin object when Player 1 or Player 2 walks or jumps on coin { World myWorld = getWorld(); myWorld.removeObject(RedCoin); Background background = (Background)myWorld; RedCounter counter = background.getRedCounter(); counter.addPoints(); GrandTotal GrandTotal = background.getGrandTotal(); GrandTotal.addRedPoints(); } Actor BlueCoin = getOneIntersectingObject(BlueCoin.class); // Actor class allocates Coin subclass to be used as a removable object and a method of points collection if (BlueCoin!=null) // Method used to remove coin object when Player 1 or Player 2 walks or jumps on coin { World myWorld = getWorld(); myWorld.removeObject(BlueCoin); Background background = (Background)myWorld; BlueCounter counter = background.getBlueCounter(); counter.addPoints(); GrandTotal GrandTotal = background.getGrandTotal(); GrandTotal.addBluePoints(); } checkKeyPress(); checkLeftWalls(); checkRightWalls(); platformAbove(); checkFall(); } public void checkKeyPress() { Background theWorld = (Background) getWorld(); Erm erm = (Erm) theWorld.getErm(); if(Greenfoot.isKeyDown("a") && erm.shouldScroll == false) { move(-8); movementLeft(); } else if(Greenfoot.isKeyDown("d") && erm.shouldScroll == false) { move(8); movementRight(); } if(Greenfoot.isKeyDown("w") && jumping == false) { jump(); } } /** * This method will make the player move right a certain amount. * Entering a negative amount will cause a movement towards the left. * @param The speed of the movement */ public void move(int amount) { int x = getX() + amount; int y = getY(); setLocation(x, y); } public void movementRight() { if (getImage().equals(image1)) { // If image2 sprite is present, the sprite rotates to image3 setImage (image2); }else if(getImage().equals(image2)) { // If image3 sprite is present, the sprite rotates to image4 setImage (image3); }else if (getImage().equals(image3)) { // If image4 sprite is present, the sprite rotates to image5 setImage (image4); }else if (getImage().equals(image4)) { // If image5 sprite is present, the sprite rotates to image6 setImage (image5); }else if (getImage().equals(image5)) { // If image6 sprite is present, the sprite rotates to image7 setImage (image6); }else if (getImage().equals(image6)) { // If image7 sprite is present, the sprite rotates to image8 setImage (image7); }else if (getImage().equals(image7)) { // If image8 sprite is present, the sprite rotates to image9 setImage (image8); }else if (getImage().equals(image8)) { // If image9 sprite is present, the sprite rotates to image10 setImage (image9); }else if (getImage().equals(image9)) { // If image10 sprite is present, the sprite rotates to image11 setImage (image10); }else if (getImage().equals(image10)) { // If image7 sprite is present, the sprite rotates to image8 setImage (image11); }else if (getImage().equals(image11)) { // If image8 sprite is present, the sprite rotates to image9 setImage (image12); }else if (getImage().equals(image12)) { // If image9 sprite is present, the sprite rotates to image10 setImage (image13); }else if (getImage().equals(image13)) { // If image10 sprite is present, the sprite rotates to image11 setImage (image14); }else if (getImage().equals(image14)) { // If image9 sprite is present, the sprite rotates to image10 setImage (image15); }else if (getImage().equals(image15)) { // If image10 sprite is present, the sprite rotates to image11 setImage (image16); }else { setImage (image3); // If image11 sprite is present, the sprite cycle loops and restarts by rotating back to image2 } } public void movementLeft() { if (getImage().equals(image1L)) { // If image2 sprite is present, the sprite rotates to image3 setImage (image2L); }else if(getImage().equals(image2L)) { // If image3 sprite is present, the sprite rotates to image4 setImage (image3L); }else if (getImage().equals(image3L)) { // If image4 sprite is present, the sprite rotates to image5 setImage (image4L); }else if (getImage().equals(image4L)) { // If image5 sprite is present, the sprite rotates to image6 setImage (image5L); }else if (getImage().equals(image5L)) { // If image6 sprite is present, the sprite rotates to image7 setImage (image6L); }else if (getImage().equals(image6L)) { // If image7 sprite is present, the sprite rotates to image8 setImage (image7L); }else if (getImage().equals(image7L)) { // If image8 sprite is present, the sprite rotates to image9 setImage (image8L); }else if (getImage().equals(image8L)) { // If image9 sprite is present, the sprite rotates to image10 setImage (image9L); }else if (getImage().equals(image9L)) { // If image10 sprite is present, the sprite rotates to image11 setImage (image10L); }else if (getImage().equals(image10L)) { // If image7 sprite is present, the sprite rotates to image8 setImage (image11L); }else if (getImage().equals(image11L)) { // If image8 sprite is present, the sprite rotates to image9 setImage (image12L); }else if (getImage().equals(image12L)) { // If image9 sprite is present, the sprite rotates to image10 setImage (image13L); }else if (getImage().equals(image13L)) { // If image10 sprite is present, the sprite rotates to image11 setImage (image14L); }else if (getImage().equals(image14L)) { // If image9 sprite is present, the sprite rotates to image10 setImage (image15L); }else if (getImage().equals(image15L)) { // If image10 sprite is present, the sprite rotates to image11 setImage (image16L); }else { setImage (image3L); // If image11 sprite is present, the sprite cycle loops and restarts by rotating back to image2 } } public boolean platformAbove() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/-2); Actor ceiling = getOneObjectAtOffset(0, yDistance, Others.class); if(ceiling != null) { vSpeed = 1; bopHead(ceiling); return true; } else { return false; } } public boolean checkRightWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/2); Actor rightWall = getOneObjectAtOffset(xDistance, 0, Others.class); if(rightWall == null) { return false; } else { stopByRightWall(rightWall); return true; } } public void stopByRightWall(Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2; setLocation(newX - 5, getY()); } public boolean checkLeftWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/-2); Actor leftWall = getOneObjectAtOffset(xDistance, 0, Others.class); if(leftWall == null) { return false; } else { stopByLeftWall(leftWall); return true; } } public void stopByLeftWall(Actor leftWall) { int wallWidth = leftWall.getImage().getWidth(); int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2; setLocation(newX + 5, getY()); } public void bopHead(Actor ceiling) { int ceilingHeight = ceiling.getImage().getHeight(); int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed <=12) { vSpeed = vSpeed + acceleration; } jumping = true; } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/2) + 5; Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Others.class); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public void jump() { vSpeed = vSpeed - jumpStrength; jumping = true; fall(); } } //Others Class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The superclass of all actors that need to scroll (i.e. all the actors except Player) * * @author Bertie Wheen * @version 0.1 */ public class Others extends Erm { public int scrollX; //The variable used when scrolling. public Others() { scrollX = 0; } /** * Used for setting the actor's location. */ public void setLocation() { setLocation(getX() + scrollX, getY()); } /** * This is used for responding to keyboard input by the user. * This will only happen when the player is far enough to the left or right of the screen for scrolling to be needed. * @param How fast the scrolling should be. 3-4 is recommended. If a negative value is used, the controls will be inverted. */ public void checkKeyPress(int amount) { Background theWorld = (Background) getWorld(); Erm erm = (Erm) theWorld.getErm(); if(Greenfoot.isKeyDown("a") && erm.shouldScroll == true) { scrollX = amount; } else if(Greenfoot.isKeyDown("d") && erm.shouldScroll == true) { scrollX = -amount; } else { scrollX = 0; } } /** * A call to the following should be in all scrolling actor's act methods. */ public void scrollingMethods() { checkKeyPress(8); setLocation(); } } Im trying to add the movement sprite method to the others class so the sprites will work while the game side scrolls
Super_Hippo Super_Hippo

2015/1/16

#
Use the code tags to post code please. What I would suggest first, because this code is a mess (and it isn't easy to get an overview of what is important and what not), have the images in arrays. Then you can code that in a few lines and don't have to do something like what you did there.
You need to login to post a reply.