Back to project page ssniper-andengine.
The source code is released under:
Apache License
If you think the Android project ssniper-andengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.cladophora.ssniper.entity; //from www . j a v a 2s. c o m import com.cladophora.ssniper.BaseActivity; import com.cladophora.ssniper.SpriteManager; import com.cladophora.ssniper.scene.GameScene; import org.andengine.entity.sprite.Sprite; import org.andengine.opengl.vbo.VertexBufferObjectManager; public class Plant { public Plant instance; public Sprite sprite; public BaseActivity activity; private VertexBufferObjectManager vbom; public Plant() { instance = this; activity = BaseActivity.getSharedInstance(); vbom = activity.getVertexBufferObjectManager(); init(); } /* method for initializing a new instance * used by constructor and pool class */ public void init() { initializeSprite(); setRandomPosition(); sprite.setIgnoreUpdate(false); sprite.setVisible(true); } public void initializeSprite() { switch (GameScene.backgroundType) { case 0: sprite = new Sprite(0, 0, SpriteManager.mTree1TR, vbom); break; case 1: switch (BaseActivity.r.nextInt(6)) { case 0: sprite = new Sprite(0, 0, SpriteManager.mTree3TR, vbom); break; case 1: sprite = new Sprite(0, 0, SpriteManager.mPlant1TR, vbom); break; case 2: sprite = new Sprite(0, 0, SpriteManager.mCactus1TR, vbom); break; case 3: sprite = new Sprite(0, 0, SpriteManager.mCactus2TR, vbom); break; case 4: sprite = new Sprite(0, 0, SpriteManager.mCactus3TR, vbom); break; case 5: sprite = new Sprite(0, 0, SpriteManager.mCactus4TR, vbom); break; } break; case 2: switch (BaseActivity.r.nextInt(2)) { case 0: sprite = new Sprite(0, 0, SpriteManager.mTree2TR, vbom); break; case 1: sprite = new Sprite(0, 0, SpriteManager.mPlant1TR, vbom); break; } break; default: sprite = new Sprite(0, 0, SpriteManager.mTree1TR, vbom); break; } sprite.setScaleCenter(0, 0); sprite.setScale(GameScene.plantScale); } public void setRandomPosition() { float x = BaseActivity.SCOPE_SIZE + (BaseActivity.r.nextFloat() * (BaseActivity.CAMERA_WIDTH - BaseActivity.SCOPE_SIZE)); float y = (BaseActivity.r.nextFloat() * BaseActivity.CAMERA_HEIGHT) - sprite.getHeightScaled(); /* // nudge back onto screen if (x + sprite.getWidthScaled() > BaseActivity.CAMERA_WIDTH) {x -= (sprite.getWidthScaled() * 0.7f) + (sprite.getWidthScaled() * 0.3f) * BaseActivity.r.nextFloat();} if (y + sprite.getHeightScaled() > BaseActivity.CAMERA_HEIGHT) {y -= (sprite.getHeightScaled() * 0.7f) + (sprite.getHeightScaled() * 0.3f) * BaseActivity.r.nextFloat();} // nudge away from scope if (x < BaseActivity.SCOPE_SIZE) {x += (sprite.getWidthScaled() * 0.6f) + (sprite.getWidthScaled() * 0.4f) * BaseActivity.r.nextFloat();} if (x < BaseActivity.SCOPE_SIZE && y < BaseActivity.SCOPE_SIZE) {y += (sprite.getHeightScaled() * 0.6f) + (sprite.getHeightScaled() * 0.4f) * BaseActivity.r.nextFloat();} */ setPosition(x,y); } public void setPosition(final float x, final float y) { sprite.setPosition(x, y); } public void clean() { sprite.setIgnoreUpdate(true); sprite.setVisible(false); //sprite.detachSelf(); sprite.clearEntityModifiers(); sprite.clearUpdateHandlers(); } }