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

2013/2/4

Making Walls :)

FuRiAx FuRiAx

2013/2/4

#
I am stuck on this and i was wandering if anyone could shed some light on my problem :)\ I cant find how to make an object that other objects cant pass through. For example in my game the player plays as a rocket and I want to make it so that it cant pass through some "wall" i have put in. I couln't think of another way soi tried adding an if(canSee(Wall.class)) clause.. public void stopatWall() { if(canSee(Wall.class)) { acceleration = new Vector(0, 0); increaseSpeed(new Vector(0, 0)); Greenfoot.stop(); (I think i need to change this part) } } with the wall being import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Wall here. * * @author (your name) * @version (a version number or a date) */ public class Wall extends Actor {public Wall() { GreenfootImage image= getImage(); image.scale(31,31); } /** * Act - do whatever the Wall 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. } This works except i cant find a command that just stops the rocket not the entire game Can anyone help? Thanks, FuRiAx
danpost danpost

2013/2/4

#
It would help to see the rocket (or player class) code. And, yes, remove the 'Greenfoot.stop();' statement.
FuRiAx FuRiAx

2013/2/4

#
Ok, its at school so ill send it tomorrow. :)
FuRiAx FuRiAx

2013/2/5

#
heres the full code for the rcket (currently its mostly taken fom asteroids because i havnt edited yet) import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A rocket that can be controlled by the arrowkeys: up, left, right. * The gun is fired by hitting the 'space' key. * * @author Poul Henriksen * @author Michael Kolling */ public class Rocket extends Mover { private int gunReloadTime; // The minimum delay between firing the gun. private int reloadDelayCount; // How long ago we fired the gun the last time. private Vector acceleration; // How fast the rocket is. private int shotsFired; // Number of shots fired. private GreenfootImage rocket = new GreenfootImage("rocket.png"); private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png"); /** * Initilise this rocket. */ public Rocket() { gunReloadTime = 20; reloadDelayCount = 0; acceleration = new Vector(0, 0.05); increaseSpeed(new Vector(13, 0.05)); // initially slowly drifting shotsFired = 0; } /** * Do what a rocket's gotta do. (Which is: mostly flying about, and turning, * accelerating and shooting when the right keys are pressed.) */ public void act() { move(); checkCollision(); checkKeys(); reloadDelayCount++; stopatWall(); } /** * Return the number of shots fired from this rocket. */ public int getShotsFired() { return shotsFired; } /** * Return the approximate current travelling speed of this rocket. */ public int getSpeed() { return (int) (getMovement().getLength() * 10); } /** * Set the time needed for re-loading the rocket's gun. The shorter this time is, * the faster the rocket can fire. The (initial) standard time is 20. */ public void setGunReloadTime(int reloadTime) { gunReloadTime = reloadTime; } /** * Check whether we are colliding with an asteroid. */ private void checkCollision() { Asteroid a = (Asteroid) getOneIntersectingObject(Asteroid.class); if(a != null) { getWorld().addObject(new Explosion(), getX(), getY()); getWorld().removeObject(this); } } /** * Check whether there are any key pressed and react to them. */ private void checkKeys() { ignite(Greenfoot.isKeyDown("w")); if(Greenfoot.isKeyDown("a")) { setRotation(getRotation() - 5); } if(Greenfoot.isKeyDown("d")) { setRotation(getRotation() + 5); } if(Greenfoot.isKeyDown("space")) { fire(); } } /** * Should the rocket be ignited? */ private void ignite(boolean boosterOn) { if(boosterOn) { setImage(rocketWithThrust); acceleration.setDirection(getRotation()); increaseSpeed(acceleration); } else { setImage(rocket); } } /** * Fire a bullet if the gun is ready. */ private void fire() { if(reloadDelayCount >= gunReloadTime) { Bullet b = new Bullet(getMovement().copy(), getRotation()); getWorld().addObject(b, getX(), getY()); b.move(); shotsFired++; reloadDelayCount = 0; } } public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public void stopatWall() { if(canSee(Wall.class)) { acceleration = new Vector(0, 0); increaseSpeed(new Vector(0, 0)); Greenfoot.stop(); } } } Thanks
TheRoshax TheRoshax

2013/2/5

#
Well, either try this: import greenfoot.*; /** * A surface for the main actor to gravitate toward. */ public class nameofclass extends Actor { /** * Creates a long platform for the main actor. */ public nameofclass() { GreenfootImage nameofclass = new GreenfootImage("nameofclass.jpg"); GreenfootImage image = new GreenfootImage(1040, nameofclass.getHeight()); int w=nameofclass.getWidth(); for(int offset=0; offset<1040; offset+=w) image.drawImage(nameofclass, offset, 0); setImage(image); } } Or this: import greenfoot.*; /** * An object to act as an obstacle. */ public class nameofclass extends Actor { /** * Creates the object and sizes its image. */ public nameofclass() { getImage().scale(40, 40); } } Hope this helps, one of them should work. TheRoshax
FuRiAx FuRiAx

2013/2/5

#
@TheRoshax It doesnt work :(
danpost danpost

2013/2/5

#
You could change your 'checkCollision' method to include the Wall as follows:
private void checkCollision() 
{
    Actor actor = getOneIntersectingObject(null);
    if(actor instanceof Asteroid) {
        getWorld().addObject(new Explosion(), getX(), getY());
        getWorld().removeObject(this);
    }
    if(actor instanceof Wall) {
        turn(180);
        move();
        turn(180);
        acceleration = new Vector(0, 0); // not sure if you need this statement
        // but, do zero the speed here
    }
}
If there is a 'setSpeed' method, use it to zero the speed. If not, and you must use 'increaseSpeed', then assuming that you have a method called 'getSpeed' that returns a Vector object, say 'increaseSpeed(-getSpeed())'.
danpost danpost

2013/2/5

#
Looks like you might run into either a nullPointerException in your 'fire' method (around line 122 of the Rocket class or an illegalStateException in the 'canSee' method of the Mover class when the rocket hits an asteroid. The rocket is removed from the world when it hits an asteroid, so any intersect checking (or use of 'getX' or 'getY') will fail and any 'getWorld' call will return null. To avoid these possible errors, change your 'act' method in the Rocket class to:
public void act()
{
    move();
    checkCollision();
    if (getWorld()==null) return; // add this line here
    checkKeys();
    reloadDelayCount++;
    stopatWall();
}
You need to login to post a reply.