Before starting, I have to say that: I’m not good at english much (just enough to describe my idea), so if I make any mistake in spelling or English grammar, just ignore it ;)). I’m not living in a country that English is a first language (I’m living in VietNam, is there anyone here live in VietNam? )
I’ve just learned jmonkey in few days, and this is the results of my try. The exercise in page 84, make a tower defense game (just for practice and experience). But I still have some troubles in shooting effects of towers. The idea is using lines to simulate a shoot from a tower to a ‘creep’. The tower would shoot the creep each one second, the line must to last long enough to make the effect looks like a laser.
This is my method: To make the tower shoot the creep and then wait a second before continuing shooting.
private long temp = 0;
private long prevTimeUpdate = -1;
private long temp1 = 0;
private long prevTimeUpdate1 = -1;
@Override
public void controlUpdate(float f)
{
if(prevTimeUpdate == -1)
prevTimeUpdate = System.currentTimeMillis();
else{
temp = (System.currentTimeMillis() - prevTimeUpdate)/100;
temp1 = (System.currentTimeMillis() - prevTimeUpdte)/100;
if(temp == 10)
//implement to make tower shoot the nearest creep
//attach lines to shootNode
if(temp1 == 11)
//detach lines from shootNode
shootNode.detachAllChildren();
}
}
Because temp1 > temp, hence the lines exist long enough to make laser effect.
But as you can see in the video (the link below), some lines did not disappear right after I detached them from shootNode. Anyone have a good idea to remove this issue, thank you in advance.
This is the link of the video: FirstSmallGame(Demo) - YouTube
Thank you for watching and reading. By the way, I’m newbie here, nice to meet you all
Hi, i don’t own the book so i cannot help with this exact problem…
But in general.
It is better to always write complete statements including the curly brackets
if(expression){
doSomething();
}
//instead of
if(expression)
doSomething();
Mainly because it keeps the code consistent. (Maybe my personal opinion) But it also allows you to remove a line without changing the whole logic flow.
Also it is hard to follow if you use variables like temp, temp1, prevTimeUpdate, prevTimeUpdate1. Since we don’t know your code we can only guess what that code does. Having speaking names helps a lot.
Sorry, I always use brackets with if statement, but at the time I was writing this post, I also was chatting with my friends on facebook too and I was using telex , so when I pressed “{”, it would be “ơ” in Vietnamese.
I named them temp and temp1 just as an example . Sorry about this inconvenience and thank you for your advice.
I printed the result out many times, It worked accurately (I updated prevTimeUpdate and prevTimeUpdate1 in if ). But I still can not figure it out why there’re still some lines in the scene after I detach them from the shootNode. 1.This is my first post on jmonkey forum and I’m so sorry about my mistakes.
2. is there any tutorial on using app.getTimer()?
Anyway, thank you for replying soon.
Thank you zzuegg with a very helpful example. I’ll try this method and post the result soon.
Thank you david_bernard_31, we learn from mistake, and from each other as well, and I’m sure that I detach all the spatials in shootNode when temp == 11.
I wonder : know how to use timer is necessary but why there’s no that information in the first 3 chapters of the book? or the authors want us to find this information from other sources?
I don’t know, I just remember that there is an example using the argument of controlUpdate in chapter two, but until now I still don’t understand it. Could you please explaining it for me?
I read the javadoc about app.getTimer().getTimePerFrame(), it says that : “Returns the time, in seconds, between the last call and the current one.” , I print it out every update, and I know that if I use a float to sum up all of these value, it will gradually reach a certain value. But the result is … I’m wrong. The sum is up and down unpredictably. Could you explain it ? I wonder if there is a difference between the argument of the controlUpdate method and the method app.getTimer().getTimePerFrame()?
…which will tell you that the parameter is “time per frame”. To explain a bit, it’s the length of time (in fractions of a second) since the last time update was called.
(Though I see that AbstractControl is having update() delegate to controlUpdate() and the javadoc is much worse for that one… so it may not have helped much.)
This concept of “tpf” (ie: ‘time per frame’) is pretty universal in JME so it’s good to know it.
Thank you guys for helping me, the problem is solved. Now the laser-effect works perfectly.
Thank you zzuegg and david_bernard_31. Now I can move to next chapter, chapter 4.
… but there’s still some confusion about argument tpf of controlUpdate and method app.getTimer().getTimePerFrame(), could you explain it for me and close this topic?
In all of my years of JME development, I have never once had to access getTimer()… because “TimePerFrame” is passed to every update as tpf. It’s the time per frame… t… p… f… It’s the amount of time that has elapsed since the last time update was called. It measures the time between frames… thus it is the “time per frame”.
In other words, if your game is running at 60 frames per second then tpf will be 1/60th of a second.