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

2014/12/30

Need help with spawning a wall at given location

SJ15 SJ15

2014/12/30

#
Hello, hope you guys can help me out. The problem im having right now is that i have a wall. And when this wall is spawned nothing can go trough it. Now i already made the part to remove the wall. But im stuck on the part to make the wall spawn on the same location as the old wall. So nothing can go trough it again. Can anyone give me an example, or give me a few leads on how this works exactly. Been working on it for quite a bit now but nothing seems to work. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MagicWall here. * * @author (your name) * @version (a version number or a date) */ public class MagicWall extends Actor { /** * Act - do whatever the MagicWall wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int xWall = 625; private int yWall = 211; public void act() { removeWall(); makeWall(); } public void removeWall() { if(Greenfoot.isKeyDown("space")) { getWorld().removeObject(this); } } public void makeWall() { String key = Greenfoot.getKey(); if ("down".equals(key)) { getWorld().addObject(new MagicWall(), xWall, yWall); } } }
danpost danpost

2014/12/30

#
Your main problem is that once you remove the wall from the world, it no longer acts. That is, the act method no longer gets executed. So, the makeWall method will not be called. Also, when a MagicWall is in the world, pressing the "space" key will add multiple MagicWalls on top of each other. This behavior is not wanted. Instead of adding and removing the wall, you can alternate changing the state of its image between opaque and transparent. Then, when your actor comes along this wall, it can check the transparent state of its image to see if it can pass or not.
SJ15 SJ15

2014/12/30

#
Thanks for your reply, i understand what you mean but how do i make something like this (im new to coding and greenfoot) ? Is there anyway you could give me a small example on how this works as code ?. Would appriciate much!
danpost danpost

2014/12/30

#
Well, the Actor class has a 'getImage' method which returns a GreenfootImage object and the GreenfootImage class has a 'setTransparency' method. Refer to the class documentations.
You need to login to post a reply.