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

2019/3/2

if else paired with isKeyDown() issues

D_S D_S

2019/3/2

#
I have a scenario where i am making a black hole on screen suck everything toward it. I have an astronaut that would like to not get eaten by the black hole. i want him to move around freely when using the arrow keys but if no arrow key is being pressed, then he should move toward the black hole like everything else. i can easily make him move with the arrow keys OR get sucked into the black hole, but I can't get both to happen. i have the normal isKeyDown() check in a list of if statements which works fine until i try to use the getSuckedIn() method. how can i add a single else at the end to call getting sucked in to start? or do i need another way to make it happen? code looks like this right now... private void checkKeyPress() { if (Greenfoot.isKeyDown("left")) turn(-8); if (Greenfoot.isKeyDown("right")) turn(8); if (Greenfoot.isKeyDown("up")) move(2); if (Greenfoot.isKeyDown("down")) move(-2); } i can't seem to make an else statement follow all this to start the getSuckedIn() method i made which looks like this public void getSuckedIn() { turnTowards(950, 65); // where the black hole is sitting move(1); }
D_S D_S

2019/3/2

#
i actually figured out a way to do this using a variable in case anyone wonders :) i declared a variable called "pressed" and made the last step in act() set it equal to 0 like this public void act() { turnAtEdge(); checkKeyPress(); getSuckedIn(); pressed = 0; } now when i press a key to move around i have the variable changed to 1 like this private void checkKeyPress() { if (Greenfoot.isKeyDown("a")) { turn(-8); pressed = 1; } if (Greenfoot.isKeyDown("d")) {turn(8); pressed = 1; } if (Greenfoot.isKeyDown("w")) {move(2); pressed = 1; } if (Greenfoot.isKeyDown("s")) {move(-2); pressed = 1; } finally getSuckedIn() first checks the variable and if it still equals 0, then it runs, if = 1 then it is skipped over for that cycle. public void getSuckedIn() { if (pressed == 0) { turnTowards(1450, 65); // where the black hole is sitting move(1); } may not be the perfect way to go about it, but it worked. i can move around no problem but as soon as no key is pressed, it heads straight for the blackhole like i want it to. i'd still love to hear if there is another way that may be better or easier.
danpost danpost

2019/3/2

#
D_S wrote...
i'd still love to hear if there is another way that may be better or easier.
Better -- yes; easier -- no. Better would be to save the actor's precise coordinates using double values; then have the black hole drawn the actor in faster when the actor is closer. This would be independent of any other movement of the actor. This would be more realistic.
D_S D_S

2019/3/3

#
i'll have to look into that. what methods do i need to look at to see about this? sounds like Greenfoot methods to me but no idea really. i'm VERY new to this and learning as i go. overall i'm having fun and actually accomplishing something. only thing i can't seem to make work is using an animated gif for the blackhole. i have followed your previous instructions and they match a couple youtube videos i found as well. but everytime i put that code in it locks up the program and it crashes on me. looks easy enough but for some reason just won't work. still working on that a bit.
danpost danpost

2019/3/3

#
D_S wrote...
i'll have to look into that. what methods do i need to look at to see about this? sounds like Greenfoot methods to me but no idea really. i'm VERY new to this and learning as i go.
You would use code like in my QActor class or the SmoothMover class. Both are available here.
overall i'm having fun and actually accomplishing something. only thing i can't seem to make work is using an animated gif for the blackhole. i have followed your previous instructions and they match a couple youtube videos i found as well. but everytime i put that code in it locks up the program and it crashes on me.
You would need to show what error message you were getting for help on this.
D_S D_S

2019/3/4

#
i used this from the gifImage documentation. basically a cut and paste changing my gif file name in. public class BlackHole extends Actor { GifImage gifImage = new GifImage("blackhole.gif"); public void act() { setImage(gifImage.getCurrentImage()); } } after placing the blackhole actor onto the world and clicking run, it locks up and does nothing. after 10 seconds or so the world goes away and i get a message saying i tried a new scenario with nothing but the blackhole on space background but even that gives me the same error.
danpost danpost

2019/3/4

#
I doubt it is the BlackHole class that is the problem. Show code of the World subclass with the BlackHole object.
D_S D_S

2019/3/5

#
that error was produced on a new project so the world code is totally blank. all i did was make a world with the space background so i had a place to put the blackhole. but otherwise i never even opened the editor for it. i of course got the same error from my current game but since it also happened on a blank world, i figured it had to be the basic code being used by GifImage somehow. kind of ruled everything else out by trying it on a fresh blank project
danpost danpost

2019/3/5

#
D_S wrote...
that error was produced on a new project so the world code is totally blank. all i did was make a world with the space background so i had a place to put the blackhole. but otherwise i never even opened the editor for it.
Can you upload the basic world with source for review?
You need to login to post a reply.