Cocos2d update() strange behaviour - cocos2d-x

I have a strange floating (Heisenbug) behavoiur when scheduling update(float deltaTime) selector in my class MainSceneLayer: public CCLayer.
update(float deltaTime) function increases m_totalTimePassed variable (class member; m_totalTimePassed += deltaTime;) and updates CCLabelTTF content to show how much time has passed.
On Windows, this works fine. But sometimes on iPad2 or iPad3 m_totalTimePassed is updating about 2 times faster than real time is.
Requesting any help and ideas. Maybe, i've set up my scene wrong, or smth else ?

There was a mistake in my own code which caused memory corruption (rotten pointer usage).

Related

SpriteBatch.draw(Sprite, ...) throws NullPointerException in LibGDX

The title is self-explanatory... Apparently the problem is in the flush method of the SpriteBatch class on the line where it says lastTexture.bind();. I did some surfing and found out it maybe because the sprite doesn't have a texture. So I ran some tests with if-statements and I literally wrote -
if(sprite.getTexture() != null) {
sb.draw(sprite, ...);
}
And it still went to the draw line and then threw this error! Any help would be appreciated...
I found out what was wrong. The problem was actually with my box textures which I was rendering before the player. Since they were stored in the lastTexture variable in my SpriteBatch while rendering the player, I got mislead and was trying to debug the wrong code. Hope this helps if anyone else faces the same problem!

libgdx's game, Screen Delay when switch from background to foreground on android

When I press the home key and return to my game, a black screen occurs.
I read online that is because textures are lost.
So, I want to know how to keep the texture valid when I resume the game. Are there any reasons for the problem? Any advice will be appreciated.
Generally,There are two reasons for this problem.
Firstly,The resource of Texture have been disposed.
Secondly,You have load repeated resources.
My problem is the second problem.
Because I always create new world in Screen's show method.
My solution,I just need to judge whether the world is null.
I only need create the world when it is null.
Based on your reply (as an answer), you can check if the world is null by checking if it's null.
public void resume() {
If(world == null) {
System.out.println("world is null");
// create world
} else {
// do stuff when it's not null.
}
}
Of course you can reference the world by making it public static when you declare it in play state so you can use it in your core code that extends the ApplicationAdapter.
Edit: Running some tests, I guess you need to set world = null; after calling world.dispose; in the dispose() method.

How do I check if objects in a frame are loaded yet or not after a gotoAndStop(5) action?

I am facing a problem, where there is a function executed the next line right after a gotoAndStop(130) action, the function depends in its work on the objects inside this frame. but the problem is that the function seems not seeing any children.. why? why? why? why?
myMc.gotoAndStop(130);
create_waypoints(par1,par2,par3);
the function creat_waypoints depends on the drawn obstacles that exists in frame 130 inside the myMc.
it seems (but I don't know for sure) by the time that creat_waypoints executes its actions, frame 130 was just entered but not yet loaded...
What can I do? Thanks in advance..
after many experiments, I have come to a working solution but it is twisted, it had to skip an entire loop of actions and I am not happy with it, but for now it is all what I have..
myMc.gotoAndStop(2);
myMc.addEventListener(Event.ENTER_FRAME,mc_ef,false,0,true);
function mc_ef(e:Event):void {
if (e.target.loaded==null) {
e.target.loaded=true;
} else {
e.target.loaded=null;
creat_waypoints(par1,par2,par3);
e.target.removeEventListener(e.type,arguments.callee);
}
}
A simpler work-around would be including the "obstacles" and their dependencies in earlier frame where the depending logic is written. They can be included as "out-of-stage" invisible members.

as3 Variables changing impossibly

I'm having an issue with a "Point" type variable velocity changing without any calls to change it.
private function framecode(e:Event) {
trace(getVelocity().y);
tracks.gotoAndStop(2);
trace(getVelocity().y);
}
This code is part of a class called "tank" which extends the one that velocity is used in (my moving object class). velocity is a private point type variable and getVelocity() is a public access method. tracks is a named movieClip contained inside the one linked with tank. The event listener is ENTER_FRAME. There is no coding on the frames of tracks.
Somehow these two traces give different values (the first one being correct) and I can't figure out how the gotoAndStop() can possibly effect it (and hence how to fix it).
I've found that play() does not reproduce the bug but prevFrame() and nextFrame() do. As the variable is private this class should not even have access to it to change it.
Another strangeness is that if the event listener is changed to FRAME_CONSTRUCTED or EXIT_FRAME, there is massive lag and my movieClip randomly disappears after a few seconds.
Thank you for reading, any help would be appreciated.
Your velocity variable is private, so one one can access that outside of the class.
However, getVelocity() is returning a reference to your velocity variable. Once someone has that reference, they can change the values of it's properties: getVelocity().y = 3. So it's not impossible for this to happen.
One way to trouble shoot this is to add a trace() statement to/set a breakpoint in getVelocity() so you can see where it's being used.
You could do something similar w/the Point class, but you'll have to extend it, add getter/setter methods for y (that traces out when they are called), and modify your code to use the getter/setter. This might be worthwhile (its simple enough), and the act of modifying your code to use the getter might help you discover where the problem is.

Force immediate layout and paint in Swing

I cannot seem to force a layout in Swing. I have a JComponent added to a JLayeredPane and I set a border on the JComponent. Then, I want to immediately re-draw everything - not in the sense of "please do this asap" like invalidate(), but synchronously and immediately. Any help? I cannot seem to find the right method of doing this, and all my reading about invalidate(), validate(), repaint(), doLayout(), etc is just confusing me more!
According to this (see the section titled "Synchronous Painting") the paintImmediately() method should work.
The most reliable way to get Swing to update a display is to use SwingUtilities.invokeLater. In your case, it would look something like
SwingUtilities.invokeLater(new Runnable {
public void run() {
somecomponent.repaint();
}
});
I realize the 'invokelater' does not exactly sound like it does anything immediate, but in practice, events posted in this way tend execute pretty quickly compared to just calling e.g. somecomponent.repaint() directly. If you absolutely must make your control code wait for the GUI to update, then there is also invokeAndWait, but my experience is that this is rarely necessary.
See also: document on event dispatch in Swing.
This is super old, but I found a simple solution, which I'll show with a JPasswordField:
var pw = new JPasswordField();
...
pw.paint(pw.getGraphics()); // paints immediately