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

Discussions

You need to login to take part
Rss

Current Discussions

Chrizzi

Actor gets randomly stuck in ground / Spielfigur steckt im Boden fest

By Chrizzi, with no replies.
Hello, I have a Little Problem with my Scenario, I made a Little Jump 'n' run game, but the actor randomly gets stuck in the ground a bit, so if I want to jump, I Need to press the key many times, until the actor gets above the ground. Here is the Code of the actor: Hallo, ich habe ein kleines Problem mit meinem Szenario, es ist ein kleines Jump 'n' Run Spiel und das Problem ist, dass die Spielfigur manchmal im Boden feststeckt, z.B. wenn ich springe, muss ich die Sprungtaste sehr oft drücken, bis der Charakter aus dem Boden rauskommt. Hier ist das Programm für den Charaker: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.io.*; /** * Write a description of class Marcel here. * * @author (your name) * @version (a version number or a date) */ public class Marcel extends Actor { long timer = System.currentTimeMillis(); Writer writer = null; int speed = 2; int jumpHeight = 200; static boolean schuss = false; static int leben; boolean start = true; boolean gedrueckt; static boolean losgelassen = false; int fallSpeed = 5; int direction = 0; static boolean spawnItem; static int MarcelX; static int MarcelY; static int lf; static int euro; static boolean right; static boolean left; static int px; static int py; static int lvl; boolean tot; public Marcel(){ this.setImage(read("SOOS/char_texture/0x00_texture1.txt")); tot = false; leben = 1; spawnItem = false; lf = 3; right = true; left = true; } /** * Act - do whatever the Marcel 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. if(!tot){ move("left","right","space"); schuss("enter"); } kys(); crrds(); px = getX(); py = getY(); stuck(); pressSwitch(); } boolean collisionDetection(java.lang.Class thing){ Actor stuff = getOneIntersectingObject(thing); return stuff != null; } void move(String keyLeft, String keyRight, String keyJump){ if(Greenfoot.isKeyDown(keyLeft)==true&&left==true){ moveLeft(); direction = -1; if(schuss == false){ Bullet.dir = Bullet.bulletSpeed * -1; } } if(Greenfoot.isKeyDown(keyRight)==true&&right==true){ moveRight(); direction = 1; if(schuss == false){ Bullet.dir = Bullet.bulletSpeed; } } /*if(Greenfoot.isKeyDown(keyJump)){ jump(); } else { fall(); }*/ if(onGround(Jump.class)==false){ jump("space"); } if(onGround(Jump.class)==true){ spring(); } if(jump("space")==false){ fall(); } } void moveLeft(){ this.setLocation(getX() - speed, getY()); } void moveRight(){ this.setLocation(getX() + speed, getY()); } boolean jump(String jumpKey){ boolean jumping = false; if(Greenfoot.isKeyDown(jumpKey) && (onGround(Ground.class)==true||onGround(Ground_half.class)==true||onGround(Block.class)==true)){ gedrueckt = true; } else { gedrueckt = false; } if(Greenfoot.isKeyDown(jumpKey) && (onGround(Ground.class)==true||onGround(Ground_half.class)==true||onGround(Block.class)==true)){ losgelassen = true; jumping = true; return jumping; } if(!gedrueckt && losgelassen == true){ int i; for(i=0;i<jumpHeight;i++){ this.setLocation(getX(), getY() - 1); if(collisionDetection(Block.class)){ i = jumpHeight; spawnItem = true; } if(collisionDetection(Ground.class)){ i = jumpHeight; } } losgelassen = false; jumping = false; return jumping; } return jumping; } void fall(){ if(onGround(Ground.class)||onGround(Block.class)||onGround(Door.class)||onGround(Ground_half.class)){ } else { this.setLocation(getX(), getY() + fallSpeed); } } boolean onGround(java.lang.Class grnd_type){ Actor under = getOneObjectAtOffset(0, (this.getImage().getHeight() / 2)+1, grnd_type); return under != null; } void schuss(String keyShot){ Bullet.mx = getX(); Bullet.my = getY(); if(Greenfoot.isKeyDown(keyShot)){ schuss = true; } else { schuss = false; } } void kys(){ if(this.getY() >= 599){ leben--; } if(collisionDetection(Gegner1.class)){ leben--; Gegner1.gegnerleben = 0; } if(collisionDetection(Gegner2.class)){ leben--; Gegner2.gegnerleben2 = 0; } if(collisionDetection(Gegner3.class)){ leben--; Gegner3.gegnerleben3 = 0; } if(collisionDetection(Gegner4.class)){ leben--; Gegner4.gegnerleben4 = 0; } if(leben <= 0){ lf--; leben = 1; if(Checkpoint.added && Checkpoint.reX != 0 && Checkpoint.reY != 0){ this.setLocation(convertStr("SOOS/0xFF_ReX.txt"), convertStr("SOOS/0yFF_ReY.txt")); } if(!Checkpoint.added){ this.setLocation(100, 423); } } if(lf <= 0){ write("SOOS/0xFF_ReX.txt","100"); write("SOOS/0yFF_ReY.txt","423"); write("SOOS/0xAB_Leben.txt","1"); write("SOOS/0xA0_Life.txt","3"); write("SOOS/0yAA_Lvl.txt","1"); write("SOOS/0y0C_Geld.txt","0"); Greenfoot.delay(50); drawGameover(); } } void drawGameover(){ getWorld().addObject(new Gameover1(), 500, 300); getWorld().addObject(new Gameover2(), 500, 500); tot = true; this.getImage().clear(); } void crrds(){ MarcelX = getX(); MarcelY = getY(); } void setCoins(int cs){ euro = cs; } void stuck(){ if(!left&&!right){ this.setLocation(getX(), getY()-20); } } void spring(){ if(collisionDetection(Jump.class)==true&&jump("Space")==true){ for(int i=0;i<150;i++){ this.setLocation(getX(), getY() - 1); if(collisionDetection(Block.class)){ i = 100; spawnItem = true; } } } } void pressSwitch(){ if(onGround(Switch.class)){ Switch.pressed = true; Door.open = true; } } void write(String dir, String txt) { String fileName = dir; try { FileWriter fileWriter = new FileWriter(fileName); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(txt); bufferedWriter.close(); } catch(IOException ex) { System.out.println( "Error writing to file '" + fileName + "'"); } } String read(String dir) { String fileName = dir; String line = null; try { FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); while((line = bufferedReader.readLine()) != null) { return line; } bufferedReader.close(); } catch(FileNotFoundException ex) { System.out.println( "Unable to open file '" + fileName + "'"); } catch(IOException ex) { System.out.println( "Error reading file '" + fileName + "'"); } return line; } int convertStr(String dir){ String str = read(dir); int value = Integer.parseInt(str); return value; } }