This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

gremlin thread

 
guv dave
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) {

}
}
}
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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..
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!