Loading images inside Timer (with MT4J)
Posted: 24 August 2014 03:28 AM   [ Ignore ]
Rank
Joined  2014-08-23
Total Posts:  2
New Member

Hi, I’m confronted with a little problem^^°

I’m trying to create an object which extends MTRectangle (better it extends an object which extends another which extends MTRectangle, but that doesn’t matter I think) inside of a java.util.Timer. Problem is, the object itself gets created and works as it should, but the PImage, the texture I gave it is not shown - instead one can only see a white square.

I hope someone has any ideas how to solve this problem, as I don’t^^°

if (config.get("ObjectSpawn").equals("1")) {
    this
.spawnObject true;
    
this.spawnTime Integer.parseInt(config.getProperty("SpawnTime"));
    
ObjectSpawner.counter 0;
}

Timer timer 
= new Timer();

        
TimerTask task = new TimerTask() {
            
@Override
            
public void run() {
                
// Increase the counter values by one each second
                
if (spawnObject true{
                    ObjectSpawner
.counter++;
                    if (
ObjectSpawner.counter == spawnTime{
                        Object object 
= new Object(gameAppgameApp.loadImage("images" AbstractMTApplication.separator "image.png"));
                        
gameScene.getCanvas().addChild(object);
                        
counter 0;
                    
}
                }
            }
        }
;
        
timer.schedule(task01000);
    
}

Profile
 
 
Posted: 27 August 2014 08:03 AM   [ Ignore ]   [ # 1 ]
Rank
Joined  2014-08-23
Total Posts:  2
New Member

Ok, for anyone with the same problem as I had - replace:

Object object = new Object(gameAppgameApp.loadImage("images" AbstractMTApplication.separator "image.png"));
                        
gameScene.getCanvas().addChild(object);

with

gameApp.invokeLater(new Runnable() {
     
@Override
     
public void run() {
          Object object 
= new Object(gameAppgameApp.loadImage("images" AbstractMTApplication.separator "image.png"));
          
gameScene.getCanvas().addChild(object);
     
}
}
);

Now it’s working for me.

Profile