lundi 23 février 2015

onSceneTouchEvent cant access a method in another class



I've been following the tutorial for AndEngine from Matim where he creates a simple platformer game. And I'm having the hardest time trying to figure out how to get the onSceneTouchEvent to work properly.



public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
{

if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
{
player.setRunning();
}

return false;

}


I have public Player player; and in the area where I generate random platforms (The difference between the actual tutorial and my program.) I create a new player.



private void generateLevel()
{
Sprite platform, platform2, coin, player;
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

//Beginning platform will always be in the same place.
platform = new Sprite(80, 900, resourceManager.platform_region, vbom);
PhysicsFactory.createBoxBody(physicsWorld, platform, BodyType.StaticBody, objectFixtureDef);
attachChild(platform);

previousX = 80;
previousY = 900;


//Random level generation
for(int x = 0; x <= 4; x++)
{
Random random = new Random();
randX = random.nextInt(100) + 100;
previousX += randX;
randY = random.nextInt(150) + 100;
previousY -= randY;
randInt = random.nextInt(2) + 1;
platform2 = new Sprite(previousX, previousY, resourceManager.platform_region, vbom);
PhysicsFactory.createBoxBody(physicsWorld, platform, BodyType.StaticBody, objectFixtureDef);
attachChild(platform2);

if(randInt == 1)
{
coin = new Sprite(previousX + 60, previousY - 30, resourceManager.coin_region, vbom);
PhysicsFactory.createBoxBody(physicsWorld, coin, BodyType.StaticBody, objectFixtureDef);
attachChild(coin);
}

}


player = new Player(170, 820, vbom, camera, physicsWorld)
{


@Override
public void onDie()
{

}

};

attachChild(player);




}


The Player class is:



public class Player extends AnimatedSprite
{
private Body body;

public boolean canRun = false;

public Player(float pX, float pY, VertexBufferObjectManager vbom, Camera camera, PhysicsWorld physicsWorld)
{
super(pX, pY, ResourceManager.getInstance().player_region, vbom);
createPhysics(camera, physicsWorld);
camera.setChaseEntity(this);
}

public void onDie() {
}

private void createPhysics(final Camera camera, PhysicsWorld physicsWorld)
{
body = PhysicsFactory.createBoxBody(physicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 0, 0));
body.setUserData("player");
body.setFixedRotation(true);

physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false)
{
@Override
public void onUpdate(float pSecondsElapsed)
{
super.onUpdate(pSecondsElapsed);
camera.onUpdate(0.1f);

if(getY() <= 0)
{
onDie();
}

if(canRun)
{
body.setLinearVelocity(new Vector2(10, body.getLinearVelocity().y));
}
}
});
}

public void setRunning()
{

canRun = true;
final long[] PLAYER_ANIMATE = new long[] { 100, 100, 100, 100, 100, 100 };
animate(PLAYER_ANIMATE, 2, 6, true);

}

public void jump()
{
body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 12));
}


Here's the LogCat log:



02-23 17:04:28.104: E/AndroidRuntime(13459): FATAL EXCEPTION: UpdateThread
02-23 17:04:28.104: E/AndroidRuntime(13459): Process: com.gamedev, PID: 13459
02-23 17:04:28.104: E/AndroidRuntime(13459): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.gamedev.Player.setRunning()' on a null object reference
02-23 17:04:28.104: E/AndroidRuntime(13459): at com.gamedev.GameScene.onSceneTouchEvent(GameScene.java:172)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:388)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.engine.Engine.onTouchScene(Engine.java:452)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.engine.Engine.onTouchEvent(Engine.java:438)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.input.touch.controller.BaseTouchController$TouchEventRunnablePoolItem.run(BaseTouchController.java:102)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:54)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:1)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.util.adt.pool.PoolUpdateHandler.onUpdate(PoolUpdateHandler.java:88)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.input.touch.controller.BaseTouchController.onUpdate(BaseTouchController.java:62)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.engine.Engine.onUpdate(Engine.java:584)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:56)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
02-23 17:04:28.104: E/AndroidRuntime(13459): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)


Sorry for the messy LogCat.


Anyway, I can't for the life of me figure it out and all my searching online has come up with nothing. So any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire