Friday, 15 May 2015

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



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

so i'm attempting create scene in game mario party, nail dice, type thing. there's random moving question mark die, , upon touch, should randomly pick number 1 - 6, , display corresponding texture. doesn't seem either register touch, or display new die.

but far random question mark die motion seems work, , won't show "dice hit" , best way accomplish this? in, best coding practice. because seems 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: // action when button pressed down. rolldice(); break; case touchevent.action_up: // action when button pressed up. break; } homecoming 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); } }

i figured out, simple switch works 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 1 time again this.sortchildren(); homecoming true; }

java android random andengine dice

No comments:

Post a Comment