I have in my android app a code snippet that uses a loop for making/drawing rectangles.
int total_rect = 2000;//This nr should not have to be here.
float rect_positions[][] = new float[total_rect][2];
for(int i =0;i<total_rect;i++){
rect_positions[i][0] = 600 + i*300 ;
rect_positions[i][1] = rand.nextFloat() * 0.8f + 0.1f;
}
They appear every 300 DP when the game scrolls.
The reason is that i wanted it to be endless of rectangles so i dont want to set a specific nr to it.
But i have(?) to make an initial nr to run the loop. (not really sure about this...)
So what i want to know is:
Is there any other(better) way to do this because now i get REALLY low fps and it is lagging like h3ll due to all the calculations for the loop.
Maybe something in the lines of :
int total_rect = 5;
when i>4 run loop again //This is what i dont know how to code...
That should work i think.
Or maybe something like
when rect_positions>last_rectposition + 1500 run loop again //This is the one i think would be the best alternative because that would meen that the further you get the more loops it will be.
Or is it anyone that have a different idea?
Related
Im trying to make a PlantsVSZombies game. The player has initially 50 suns in order to buy plants. I want to make a counter which decreases whenever I buy plants but also increases continously if I have sunflowers present. Any ideas how I could do it? And thank you.
In pseudocode:
int counter = 50;
if(....)
...
counter--;
else if (....)
...
counter++;
It would be easier for us if you displayed what code you have tried and what issues you encountered.
For the next time, try to add some information about your classes and how you are planning to write all your code. For this question and with this information you can try to just manage that with one Integer value:
int suns = 50;
public sunsCounter(bool increase){
if(increase){
this.suns +=1
}else{
this.suns -=1
}
I'ts just an idea and it's so basic, obviously, you have to create some classes to manage that and operate with this class objects.
Is it possible to start a particle effect mid way through? I have tried many variations of updating the particle effect/emitters upon initialisation. None of them seem to work. Has anyone managed to do this before? Thanks a lot!
ParticleEffectPool.PooledEffect effect = particleEffectPool.obtain();
effect.setPosition(posnX,posnY);
float value = 1.5f;
for(ParticleEmitter e: effect.getEmitters()){
e.update(value);
value+=1.5f;
}
The above code doesn't draw all of the particles, but it does seem to update the them somewhat. Once the initial effect is over, it resets and then it looks fine
EDIT: I've found a little bit of a hack by doing the following code snippet 5 times upon initialisation of the particle effect. Still interested to see if someone has a better solution
p.getEmitters().get(0).addParticle();
p.update(1);
I assume, that all emitters in your ParticleEffect have the same duration:
ParticleEffectPool.PooledEffect effect = particleEffectPool.obtain();
effect.reset();
effect.setPosition(posnX,posnY);
//divide by 1000 to convert from ms to seconds
float effectDuration = effect.getEmitters().first().duration / 1000f;
float skipProgress = 0.5f;
effect.update(skipProgress * effectDuration);
Note, that if emitters have different duration, you probably would want to pick the max duration. Also, if your emitters have delays, you should take them into account too.
Update
This approach will not work as expected in case, when some of effect's properties change over time. So if you skip half of its duration, you don't take in account all changes that happened before. You just start from some state.
For example, let's say effect has duration = 10, and its velocity is 100 for the first 4 seconds, and after that velocity is 0. If you call effect.update(5), i.e. just skip first 5 seconds, particles will have velocity = 0, they just won't "know", that they had to move for the first 4 seconds.
So, I guess the only workaround here, is to update the effect with small steps in a loop, instead of just updating for half of its duration in one call:
ParticleEffectPool.PooledEffect effect = particleEffectPool.obtain();
effect.reset();
effect.setPosition(posnX,posnY);
//divide by 1000 to convert from ms to seconds
float skipDuration = 0.5f * effect.getEmitters().first().duration / 1000f;
//I guess, to reduce number of iterations in a loop, you can safely use
//a bit bigger stepDeltaTime, like 1 / 10f or bigger, but it depends on you effect;
//here I just use standard frame duration
final float stepDeltaTime = 1 / 60f;
while (skipDuration > 0) {
float dt = skipDuration < stepDeltaTime ? skipDuration : stepDeltaTime;
effect.update(dt);
skipDuration -= stepDeltaTime;
}
I'm completely new to Processing, very enthusiastic, but really stumped at the first hurdle. I really appreciate the fact that this is surely very basic stuff, but would be so happy if anyone could give me a hand to move on a bit.
I'm looking for a way to input a long-ish list of words which will then display one by one, with each appearing for 1 second and then disappearing.
I've found a way to print a whole sentence and have words disappear individually, and tried the below which seems to work a little better. The problem with this is I can't work out how to add more words to the loop, it seems to only consider one against the other. Is there an entirely different approach I can take?
Many thanks, this is where I'm up to
String Carol = "Carol";
String Charlotte = "Charlotte";
String Ellen = "Ellen";
String displayed ="";
int interval = 1000; // s
int time;
PFont font;
void setup() {
size(500, 500);
font = createFont("arial", 44);
background(0);
displayed = Carol;
time = millis();
textFont(font);
fill(255);
}
void draw() {
background(0);
text(displayed, width/2 - textWidth(displayed)/2, height/2);
if (millis() - interval > time) {
displayed = displayed.equals(Carol)? Charlotte:Carol;
time = millis();
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense.
You need to break your problem down into smaller steps and then take those steps on one at a time. For example, can you write a simple example program that just shows a single hard-coded string? Now can you make it so the string disappears after 1 second? Then try to add a second hard-coded string that appears after the first one.
Then if you get stuck, you can post a MCVE along with a specific technical question. Good luck.
Hint: You can probably use the millis() function or the frameCount variable. The Processing reference is your friend. But again, start simple and work your way forward in small incremental steps!
I'm working on a little project where I want to implement a little delay in a loop, but I don't want to use a thread or use another class. Is this possible?
My code looks like this:
int random = (int)(Math.random()*15);
int randomloop;
for (i = 0; i < random; i++){
randomloop = (int)(Math.random()*15);
nummerlabel.setText(String.valueOf(randomloop)); //showing a number
//here i would like to implement a little delay, so the number changes every 0.5 seconds
}
Seeing as you are already using Math and String I am assuming you are not opposed to using the standard library and you simply want to avoid creating a new thread. In that case, use Thread.sleep(...).
I am using this code structure below from here http://www.koonsolo.com/news/dewitters-gameloop/
to set a game loop that processes based on a set fps but renders/draws at the most possible.
How would one implement a cap on the drawing fps so as not to use up all the processing power /battery life. or to limit it for v-syncing.
const int TICKS_PER_SECOND = 60;
const int SKIP_TICKS = 1000000000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;
DWORD next_game_tick = GetTickCount();
int loops;
float interpolation;
bool game_is_running = true;
while( game_is_running ) {
loops = 0;
while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
update_game();
next_game_tick += SKIP_TICKS;
loops++;
}
interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
/ float( SKIP_TICKS );
display_game( interpolation );
}
I assume that you are actually doing proper motion interpolation? Otherwise it doesn't make sense to render faster than your game update: you'll just be rendering all the objects again in exactly the same position.
I'd suggest the following:
Put a Thread.sleep(millis) call in to stop the busy-looping. Probably a Thread.sleep(5) is fine, since you are just going to do a quick check for whether you are ready for the next update.
Put a conditional test on the display_game call to see if at least a certain number of millisconds has elapsed since the last display_game. For example, if you make this 10ms then your frame rate will be limited to 100 FPs.
There are also a couple of other things that are a bit unclear in your code:
What is DWORD? Is this really Java? Looks like some funny C/C++ conversion? The normal way to get the current time in Java would be long time=System.nanoTime() or similar.....
What graphics framework are you using? If it is Swing, then you need to be careful about what thread you are running on, as you don't want to be blocking the GUI thread....
Finally, you should also consider whether you want to decouple your update loop from the rendering code and have them running on different threads. This is trickier to get right since you may need to lock or take snapshots of certain objects to ensure they don't change while you are rendering them, but it will help your performance and scalability on multi-core machines (which is most of them nowadays!)
I think you can update your display_game to compare the FPS being painted against the desired limit. If it has reach that limit, you can add a wait time for wait time as:
Thread.sleep(500); //wait for 500 milliseconds