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

2017/4/23

I NEED IMMEDIATE HELP

1
2
3
4
Xmin_Terminator Xmin_Terminator

2017/4/23

#
I would preferably need help in the next 12 hours if possible. I am trying to make a maze game with "lasers" that you need to deactivate by running over the "buttons". I have created two images for active and not "lasers". I have managed to make to deactivated "laser" image appear when the button is hit by the player, but I don't know how to remove the other "laser" that is underneath it that is preventing the player from walking through. I do not mind re-locating its position to another position on the map as I am able to create an area where I can put them. could you please try and help me, I will show you all the code that I have that could come of use. Thanks a lot if you can help me. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) PLAYERS CODE /** * The character that the player of the game plays at to complete the maze * Xmin_Terminator * @version (a version number or a date) */ public class Player extends Actor { public void act() { movement(); hitButton1(); } public Player() //divides the players dimensions by 3 to scale down to fit inside the maze { GreenfootImage myImage = getImage(); int myNewHeight = (int)myImage.getHeight()/3; int myNewWidth = (int)myImage.getWidth()/3; myImage.scale(myNewWidth, myNewHeight); } public void movement() { if(Greenfoot.isKeyDown("right")) { setRotation(0); if(getOneObjectInFront(Wall.class)==null) //if all 3 of these are not in front of the player, then the player is allowed to move if(getOneObjectInFront(Maze.class)==null) //the speed is set to 5 so I can test it quicker, I will turn it down to 3 later if(getOneObjectInFront(LaserWall.class)==null) move(5); } if(Greenfoot.isKeyDown("left")) { setRotation(180); if(getOneObjectInFront(Wall.class)==null) if(getOneObjectInFront(Maze.class)==null) if(getOneObjectInFront(LaserWall.class)==null) move(5); } if(Greenfoot.isKeyDown("up")) { setRotation(270); if(getOneObjectInFront(Wall.class)==null) if(getOneObjectInFront(Maze.class)==null) if(getOneObjectInFront(LaserWall.class)==null) move(5); } if(Greenfoot.isKeyDown("down")) { setRotation(90); if(getOneObjectInFront(Wall.class)==null) if(getOneObjectInFront(Maze.class)==null) if(getOneObjectInFront(LaserWall.class)==null) move(5); } } private Actor getOneObjectInFront(Class c) //checking if there is a certain actor in front of itself { GreenfootImage myImage = getImage(); int distanceToFront = myImage.getWidth(); int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation()))); int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation()))); return (getOneObjectAtOffset(xOffset, yOffset, c)); } public boolean buttonSwitcher = false; //I believe this is the issue, it is not working inside the other actors editors or I don't know how to fix it. public void hitButton1() { World myWorld = getWorld(); if(getOneIntersectingObject(Button1.class)!= null) { DeactivatedLasers deactivatedLaser1 = new DeactivatedLasers(); myWorld.addObject(deactivatedLaser1, 12+(1*25),12+(6*25)); buttonSwitcher = true; } } } LaserWall 1-20 are a sub-class of LaserWall LASERWALL CODE public class LaserWall extends Actor { public boolean buttonSwitcher = false; // I thought this may link them up but I don't know how this works and I don't think It linked up /** * Act - do whatever the LaserWall wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // } } laserwall1 is the first one that I am trying to make this code work one, then I will apply that it the other codes so that I can complete it. So because of this I may not say, assign something to the upper class Laser Actor, as this needs to works for all of my actors, LaserWall 1 throughout to 20 LASERWALL1 CODE public class LaserWall1 extends LaserWall { public boolean buttonSwitcher = true; // I thought this may connect the Boolean from the player code, but I don't think it did /** * Act - do whatever the LaserWall1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(buttonSwitcher != true) { setLocation(12+(25*25), 12+(25*25)); //This is the "disposal" location, where I would like to dump the activated lasers away. } } } I know this is a big favour I am asking for but would be really grateful if somebody could see to my need quickly, thanks a lot, hoping to get some advice and major help quickly.
Super_Hippo Super_Hippo

2017/4/23

#
You only need one class for the lasers and one class for the buttons, not one class for each object. Every object will have its own "buttonSwitcher" variable (the name is the same, but it is not the same variable), so you can't do it like this. Instead, you will have something like this:
//Button code
private Laser laser;

//create the laser, then create the button and pass the laser to its button
public Button(Laser connectedLaser)
{
    laser = connectedLaser;
}

public void toggleLaser()
{
    laser.toggle();
}
//in Laser
private boolean activated = false;

public void toggle()
{
    activated = !activated;
    //change image
}
//in Player
Button button = (Button) getOneIntersectingObject(Button.class);
if (button != null)
{
    button.toggleLaser();
}
Hope this helps.
Xmin_Terminator Xmin_Terminator

2017/4/23

#
Thanks, I am about to try this out, but a question, do I need multiple actors for the deactivated laser actors, or do the same as I am doing for the lasers that are active and the button. thanks a lot for quickly replying. And also do I add the lasers and the button in the world sub-class background ( I only am using one background and one subclass) or do I have to add them in a sub-class from an actor.
Xmin_Terminator Xmin_Terminator

2017/4/23

#
also how do I do the code to change the image I do not know how to do the //create the laser, then create the button and pass the laser to its button properly, and I don't know how to do the //change image properly, please could you give me guide to doing this I have entered in exactly what you said apart from those things, and I get errors in the player code This is probably because I don't know how to out the two codes in as above, but here is the errors.
Button button = (Button) getOneIntersectingObject(Button.class);
   if // illegal start of type  (button // <identifier> expected != //; expected null) //illegal start of type
   {// ; expected
       button. //illegal start pf type toggleLaser();
   }
} // class interference or enum expected
// the last parenthesis is for the end of code, the green one 
sorry about the code before, I just learn how to use this to make it clearer
danpost danpost

2017/4/23

#
I am not sure why you have two images for the laser. I can understand two images for the buttons -- one for each state of the button (on or off). But what you should have is one image for the actor which will produce a beam (whether on or off) and another actor for the beam itself. Then, when the button toggles the state of the laser (or deactivates it), the laser can just add the beam in or remove it from the world.
Xmin_Terminator Xmin_Terminator

2017/4/23

#
What I want is the lasers o be red when they are active, and then to be green when they are deactivated. And they are deactivated when the button is pressed, but I am struggling to get this switch between them, as the red Laser will not let the player past, but the green laser will. I am not sure how to do this, I am fine by just moving the red laser out of the way to another position, as I can create a disposal point.
Xmin_Terminator Xmin_Terminator

2017/4/23

#
What I want is the lasers o be red when they are active, and then to be green when they are deactivated. And they are deactivated when the button is pressed, but I am struggling to get this switch between them, as the red Laser will not let the player past, but the green laser will. I am not sure how to do this, I am fine by just moving the red laser out of the way to another position, as I can create a disposal point.
danpost danpost

2017/4/23

#
Well, I think you misplaced the code Hippo provided for the Player class -- it should go within the act method of the class.
Xmin_Terminator Xmin_Terminator

2017/4/23

#
Ok I will try out right now, but he said something about connecting the lasers and button and setting some image that I am unsure about how to do.
danpost danpost

2017/4/23

#
Line 7 of the laser class code supplied by Hippo (// change image) would be something like:
if (activated) setImage("redLaser.png"); else setImage("greenImage.png");
danpost danpost

2017/4/23

#
You said (in a round-about way) that you had 20 lasers. Does each laser have a distinct button that deactivate them (are there also 20 buttons)?
Xmin_Terminator Xmin_Terminator

2017/4/23

#
By putting the code in the public act, for player, the code in the button says cannot fine symbol - class Laser, for the private Laser laser; and says cannot find symbol - variable connectedLaser for
public Button(LaserWall connectdLaser)
    {
        laser = connectedLaser;
    }
it also has errors when adding the buttons in the background sub class of the world, example :
Button button1 = new Button();
// underlining the new button section saying 
//constructor Button in class Button cannot be 
//applied to given types;
//required: LaserWall
//found: no arguments
//reason: actual and formal argument lists differ in 
//length
Please could you help me with this Maybe you could tell me how to upload a scenario, then you can check it out for yourself and it may be easier for you to understand and explain my problem to me, also show me how to fix it
Xmin_Terminator Xmin_Terminator

2017/4/23

#
let me try putting the line 7 in
Xmin_Terminator Xmin_Terminator

2017/4/23

#
How do I set the images as the names redLaser and greenLaser
danpost danpost

2017/4/23

#
Xmin_Terminator wrote...
How do I set the images as the names redLaser and greenLaser
You do not have to change the name of your images -- just change the names I gave to match those that you have.
There are more replies on the next page.
1
2
3
4