• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

gremlin thread

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically i got a maze with a player and a gremlin. the gremlin moves very fast through the cells hence i put it to sleep for a second at each cell. but i get the problem that when my player walks past it when it is asleep, then the player does not die. but when he goes pat it when its awake it dies. i want it to go slowly through the maze hence stay asleep but yet still kill my player. i tried doing this by returning where the gremlin is and matchin it with the player but this also works only when the gremlin is not sleep. please help . its my first year in java. thanks

public void run()
{
while (true) {
getMaze().moveGremlins();
GremlinsEffect();
// game.view().repaint();
try {
this.sleep(500);
} catch (Exception e) {

}
}
}
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggestion: Instead of having the Gremlin check for the player every half second, how about have the player check for the Gremlin everytime it moves?

Henry
 
guv dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void GremlinsEffect()
{
// int x = gremlin.getX();
// int y = gremlin.getY();
int xPlayer = player.getX();
int yPlayer = player.getY();
List<Gremlin> gremlins = getGremlins();
for (Gremlin gremlin : gremlins)
{
if(xPlayer==gremlin.getX() && yPlayer==gremlin.getY())
//return gremlin;
{
player.decreaseStars(1);
if (player.getStars() == -1)
{
JOptionPane.showMessageDialog(frame, "you have lost all your stars!! Game Over!!");
System.exit(0);
}
else {
JOptionPane.showMessageDialog(frame, "You have been Killed by the Gremlin ");
setPlayerStart();
}
}

as you can see i am doing that. i think. however the effects only happen when the gremlin aint sleeping. there must be a way round this..
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by guv dave:
as you can see i am doing that. i think. however the effects only happen when the gremlin aint sleeping. there must be a way round this..



Did you check to see whether the condition:

returns true?
(Crudely speaking, you can put a print statement there to see whether the code enters the code block or not.)
It does not look like that the sleeping of gremlin will have an effect on the code you have shown unless setPlayerStart() or getX() and getY() of gremlin depends on it.
 
guv dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i worked it out, i was only calling the effect when the gremlin move about. i also put the effect in when the [player moves about so it will kill it. thanks
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by guv dave:
yes i worked it out, i was only calling the effect when the gremlin move about. i also put the effect in when the [player moves about so it will kill it. thanks



Yep. That's what Henry said to do!
 
guv dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol thanks henry, da thing is he said it but it took me quite a bit of time to understand it lol.

thanks mate!
 
    Bookmark Topic Watch Topic
  • New Topic