The code was:
open_window();
for(i=0;i<100000;i++){
clear_the_window();
draw_frame(i);
wait_until_a_24th_of_a_second_is_over();
}
The book says the problem with this code is: Suppose the drawing takes nearly a full 1/24 second. Items drawn first are visible for the full 1/24 second and present a solid image on the screen; items drawn toward the end are instantly cleared as the program starts on the next frame.
I don't quite understand what does it mean by "first" and "toward the end"? If the three functions within the loop are called sequentially, what is the problem? Unless it is not a sequential program?
Say this span of 10 stps covers 1/24th of a second:
Clear window
Item A begin drawing...
A is drawn completely and visible for almost 1/24th second
Item B begin drawing ...
...
... expensive drawing of B ...
B finished drawing -> wont be shown too long
Item C begin drawing...
...
finished drawing C -> will last very short as next iteration is
immanent
------- NEXT iteration -------
Clear window
and so on
Related
Code here: https://gist.github.com/protectivetoast83/25301772cbc6f13e6e07
images of problem
The program should be displaying a single gray rectangle at coordinates 32, 32 on the window however it was also displaying a second one at coordinates 0,0.
I added a cout statement on line 60 of gameWorld.cpp and the program started working correctly. The program works correctly only so long as I have that cout statement there.
What's going on and how can I fix it?
Line 57 of gameWorld.cpp looks suspicious:
if(objSet[i].checkActive());
That semicolon ends the if statement ("if object i is active, do nothing"), and the subsequent four lines are a bare inner block that always gets executed. (This explains why, in your second screenshot, you're looping over all objects from i=0 to 99 as shown by the text box in the lower-right corner. It doesn't tell me why adding line 60 changes your code's behaviour, though.)
I'm working on a game project in c++ using programming oriented object and classes but I can't figure out a way to animate the following graphics.
What I need is while the player is holding left key or the right key, the graphics should be appearing to make the character like moving and when they stop holding the key it'll turn to idle graphic.
I can't paste the whole source code here, i have many classes, and functions.. All I need is a BASIC idea of how to implement it, an example or a function anything useful. I don't need libraries because i just have two sprites to animate so it's not necessary.
As an example be Sprites the class that creates the object and Koala the one that moves it and prints it in a certain position.
Sprites idleSprite, walkingSprite;
Koala koala;
These declarations are just for avoiding other explanations.
I would appreciate your help.
PD: Don't worry about the keyboard keys, or other classes all I need is how to animate a sprite.
Koala should have two states:
a direction state: enum Direction {Left,Right};
a movement state. enum Movement { Idle, Walk };
As you have only one picture for the walking status, moving graphically the picture around will give the impression of a floating body. I'd recomment that you'd really have at least two walking positions to show that the foots are moving:
a movement step counter
a constant for the maximum number of steps.
Then the states should be updated in your game loop according to keyboard status and elapsed time. The pseudo code would be something like:
if (!arrow_key_pressed()) {
status_movement = Idle;
lasttimer = gametimer(); // keep track of last event
}
else {
status_movement = Walk;
if (left_arrow_pressed() )
status_direction = Left;
else if (right_arrow_pressed() )
status_direction = Right;
if (gametimer() - lasttimer > 2 ms ) { // if enough time,
if (status_direction==Left)
position_x -= step_increment;
else if (status_direction==Right)
position_x += step_increment;
movement_step = (movement_step+1) % maxi_steps;
lasttimer = gametimer();
}
}
All you have then to do is to restore te background of the picture at tis old position, and draw the picture at the position. For this, youd could call a function with paramters position_x, direction, movement status and status step, to return/draw the sprite at the right place.
My hero sprite is on a physics sprite. If the player (user) doesn't move the hero for 2 seconds while touching the sprite I want to end the game. In the update method I am checking to see if the two sprites are touching for longer than 2 seconds and if true run "game over" action. This is the code
if( TWO SPRITES ARE TOUCHING && USER ISN"T TOUCHING THE SCREEN ){
[_hero runAction:[CCActionSequence actions:[CCActionDelay actionWithDuration:2.0f],
_gameOverAction,
nil]];
}
The end game action runs even if the user is holding down. Basically if you don't touch the screen to move the sprite the game should end. Any suggestions?
In your current code as soon as two sprites are touching and the user isn't touching the screen it will quick off the action. It will happen in 2 seconds no matter what as it is never cancelled.
What you need to do instead is keep a variable of the time it last happened and check to see if 2 seconds have passed since then in your update loop. If it has then you can call the game over method.
I'm using a particles, slightly modified CCParticleFlower with positionVar to be in form of vertical line, from top to bottom. In CCNode update I constantly change the position of the particles from left to right across the whole screen, when it reaches the right side I set x to 0 and start scrolling to the right.
The problem is when I reset the X value to 0, all particles blinks, they disappear for about one frame and appear in the next frame, it causes a nasty flickering effect.
It does not happen when I increment X values by small numbers but when the particle position is reset to its beginning position it flickers, on win32, android and ios. I’m using most recent 1.1 version (master branch)
I recently had something of a similar problem where the particles would jump around whenever their parent changed direction. I'm not sure if it's exactly the same problem, but here's the thread I found that helped with my problem:
http://www.cocos2d-iphone.org/forum/topic/17167
The relevant post:
I just encountered the same problem and it took me a while to get to the bottom of it, >here's the low down: do not use
[self schedule:#selector(NextFrame:)];
Instead, use
[self scheduleUpdate];
and rename NextFrame: to update:
Using a custom selector schedules your update at the very end of the CCScheduler queue, in other words, it will cause your NextFrame: method to be called AFTER the particle system's update: method, because the particle system schedules its own update method with a priority of 1.
This is not good because the position of the quads for the particles are updated using the current position of the emitter, and then the emitter is moved in your NextFrame: method, which causes all the particles to be moved again because the position of the emitter is really the position of the CCNode that draws the particles.
By using scheduleUpdate, you really schedule your update: method with a priority of 0, which means it will be called before the particle system's update: method and all will be well.
So basically, add an update method to your class and call scheduleUpdate instead of manually scheduling it.
I have designed a small tutorial named "Stacker", As the name suggests, The game involves stacking blocks on each other. I have a large number of blocks to be stacked and hence all cant be accomodated in the screen itself... I m new to cocos2d and box2d but have managed to create a body with its adjoining sprite wen a user clicks on the screen. I have used MouseJoint to give movement to the body till the user performs the drag action that is till the user takes his finger off the screen.
The problem is that i need to follow the sprite (actually need the camera to follow the sprite) when the user drags it above the screen space, i referred the following links with no success... i guess wat i need is to move the body to a virtual coordinates which m not getting coz even if the screen does shift using the camera methods, but the sprite doesnt move with respect to the screen...
cocos2d forum link
flash concept but box2d
Can some1 guide me in case i need to have some pre-requisites before following camera in the manner i specified.. Thanx!
Ok Guys!
Got it guys! Had to take a global variable which records the increments per frame, The increments were equal to the layer movement which i did by setting the position of the layer to a unit less in every frame! Then set the Mouse join to the target which is (ScreenCoordinates + increment) dis too has to be done in every frame!
Done!
Cool method but needed a bit of brainstorming!!