Search code examples
javaandroidrandomandenginedice

How to get this dice code to display the new rolled die in AndEngine?


So I'm attempting to create a scene in my game like the Mario Party, hit the dice, type thing. But there's a random moving question mark die, and upon touch, should randomly pick a number 1 - 6, and display the corresponding texture. But it doesn't seem to either register the touch, or display the new die.

But so far only the random question mark die movement seems to work, and won't show any "dice hit" And what would be the best way to accomplish this? As in, best coding practice. Because this seems a little off..

public class Dice extends Sprite implements IEntity {
private final PhysicsHandler mPhysicsHandler;
Random randomGenerator = new Random();
private float RandomX;
private float RandomY;
private int CAMERA_WIDTH = 960;
private int CAMERA_HEIGHT = 540;

public static Sprite newDiceSprite;

public int mDiceHit;

public Dice(final float pX, final float pY,
        final ITextureRegion pTextureRegion,
        final VertexBufferObjectManager pVertexBufferObjectManager) {
    super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
    this.mPhysicsHandler = new PhysicsHandler(this);
    this.registerUpdateHandler(this.mPhysicsHandler);
    RandomX = randomGenerator.nextInt(3);
    RandomY = randomGenerator.nextInt(3);
    RandomX = RandomX * 100;
    RandomY = RandomY * 100;
    this.mPhysicsHandler.setVelocity(RandomX, RandomY);
}

@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
    if (this.mX < 0) {
        this.mPhysicsHandler.setVelocityX(RandomX);
    } else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
        this.mPhysicsHandler.setVelocityX(-RandomX);
    }

    if (this.mY < 0) {
        this.mPhysicsHandler.setVelocityY(RandomY);
    } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
        this.mPhysicsHandler.setVelocityY(-RandomY);
    }

    super.onManagedUpdate(pSecondsElapsed);
}

@Override
public boolean onAreaTouched(final TouchEvent pAreaTouchEvent,
        final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
    switch (pAreaTouchEvent.getAction()) {
    case TouchEvent.ACTION_DOWN:
        // do action when button pressed down.

        rollDice();

        break;
    case TouchEvent.ACTION_UP:
        // do action when button pressed up.
        break;
    }
    return super.onAreaTouched(pAreaTouchEvent, pTouchAreaLocalX,
            pTouchAreaLocalY);
}

public void rollDice() {

    Random rand = new Random();

    mDiceHit = (rand.nextInt(5) + 1);

    if (mDiceHit == 1) {

    } 

    else if (mDiceHit == 2) {

    }

    else if (mDiceHit == 3) {

    }

    else if (mDiceHit == 4) {

    } 

    else if (mDiceHit == 5) {

    }

    else if (mDiceHit == 6) {

        //newDiceSprite = new Sprite(0,0, ResourceManager.dieSideSixTextureRegion, ResourceManager.getInstance().engine.getVertexBufferObjectManager());
        //newDiceSprite.setPosition(400, 200);
        //ManagedGameScene.attachChild(newDiceSprite);
        GameLevel.newDice6();

    }

}

}

public class GameLevel extends ManagedGameScene {

public static Sprite diceSprite;

@Override
public void onLoadScene() {
    super.onLoadScene();
    Rectangle rectangle = new Rectangle(0f,0f,120f,120f,ResourceManager.getInstance().engine.getVertexBufferObjectManager());
    rectangle.setPosition(MathUtils.random(0f+rectangle.getWidth(),(800f-rectangle.getWidth())), MathUtils.random((-240f+rectangle.getHeight()),(240f-rectangle.getHeight())));
    //this.attachChild(rectangle);

    Random rand = new Random();



    diceSprite = new Dice(rand.nextInt(500), rand.nextInt(350),
            ResourceManager.questionDiceTextureRegion,
            ResourceManager.getInstance().engine.getVertexBufferObjectManager());

    diceSprite.setPosition(400, 200);

    this.attachChild(diceSprite);
    this.registerTouchArea(diceSprite);
    //chickenSprite.setVisible(true);



}

public static void newDice6() {
    // TODO Auto-generated method stub

    Random rand = new Random();

    diceSprite = new Dice(0, 0,
            ResourceManager.dieSideSixTextureRegion,
            ResourceManager.getInstance().engine.getVertexBufferObjectManager());

    diceSprite.setPosition(400, 200);

    SceneManager.getInstance().mCurrentScene.attachChild(diceSprite);

}
}

Solution

  • I figured it out, a simple switch works just fine.

    public boolean Roll() {
    
        ResourceManager.diceShake.play();
    
        Random rand = new Random();
    
        switch (rand.nextInt(6) + 1) {
        case 1:
            // dice_picture.setImageResource(R.drawable.one);
            dice1Sprite = new Dice(20, 20,
                    ResourceManager.dieSideOneTextureRegion,
                    ResourceManager.getInstance().engine
                            .getVertexBufferObjectManager());
    
            this.attachChild(dice1Sprite);
    
            break;
        case 2:
            // dice_picture.setImageResource(R.drawable.two);
            dice2Sprite = new Dice(40, 40,
                    ResourceManager.dieSideTwoTextureRegion,
                    ResourceManager.getInstance().engine
                            .getVertexBufferObjectManager());
    
            this.attachChild(dice2Sprite);
    
            break;
        case 3:
            // dice_picture.setImageResource(R.drawable.three);
    
            dice3Sprite = new Dice(60, 60,
                    ResourceManager.dieSideThreeTextureRegion,
                    ResourceManager.getInstance().engine
                            .getVertexBufferObjectManager());
    
            this.attachChild(dice3Sprite);
    
            break;
        case 4:
            // dice_picture.setImageResource(R.drawable.four);
    
            dice4Sprite = new Dice(80, 80,
                    ResourceManager.dieSideFourTextureRegion,
                    ResourceManager.getInstance().engine
                            .getVertexBufferObjectManager());
    
            this.attachChild(dice4Sprite);
    
            break;
        case 5:
            // dice_picture.setImageResource(R.drawable.five);
    
            dice5Sprite = new Dice(100, 100,
                    ResourceManager.dieSideFiveTextureRegion,
                    ResourceManager.getInstance().engine
                            .getVertexBufferObjectManager());
    
            this.attachChild(dice5Sprite);
    
            break;
        case 6:
            // dice_picture.setImageResource(R.drawable.six);
    
            dice6Sprite = new Dice(120, 120,
                    ResourceManager.dieSideSixTextureRegion,
                    ResourceManager.getInstance().engine
                            .getVertexBufferObjectManager());
    
            SceneManager.getInstance().mCurrentScene.attachChild(dice6Sprite);
    
            break;
        default:
        }
    
        rolling = false; // user can press again
    
        this.sortChildren();
        return true;
    
    }