Andengine problems

Problems with Canvas, OpenGL, etc...

Andengine problems

Postby Axeves » Sat Jan 01, 2011 1:27 am

I realize i should post this in the AndEngine forums, but i just wanted some answer this late in the night, and this is probably some simple error, but i can't seem to figure it out.

i get these errors:

The method addEntity(Game.Ball) is undefined for the type Layer


And 3 more like the one above, one of them being removeEntity

If it helps, i have another activity that switches to this activity with Intents.

Help please? :)

Entire Game.java below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package splash.screen;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.anddev.andengine.engine.Engine;
  7. import org.anddev.andengine.engine.camera.Camera;
  8. import org.anddev.andengine.engine.handler.IUpdateHandler;
  9. import org.anddev.andengine.engine.options.EngineOptions;
  10. import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
  11. import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
  12. import org.anddev.andengine.entity.scene.Scene;
  13. import org.anddev.andengine.entity.scene.background.ColorBackground;
  14. import org.anddev.andengine.entity.sprite.AnimatedSprite;
  15. import org.anddev.andengine.entity.sprite.Sprite;
  16. import org.anddev.andengine.entity.util.FPSLogger;
  17. import org.anddev.andengine.input.touch.TouchEvent;
  18. import org.anddev.andengine.opengl.texture.Texture;
  19. import org.anddev.andengine.opengl.texture.TextureOptions;
  20. import org.anddev.andengine.opengl.texture.region.TextureRegion;
  21. import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
  22. import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
  23. import org.anddev.andengine.ui.activity.BaseGameActivity;
  24.  
  25. import android.widget.Toast;
  26.  
  27. public class Game extends BaseGameActivity {
  28.  
  29.     private static final int CAMERA_WIDTH = 720;
  30.     private static final int CAMERA_HEIGHT = 480;
  31.     private static final float DEMO_VELOCITY = 250.0f;
  32.  
  33.     private Camera mCamera;
  34.     private Texture mTexture;
  35.     private TextureRegion mPongTextureRegion;
  36.     private TextureRegion mBoxTextureRegion;
  37.     private TiledTextureRegion mBallTextureRegion;
  38.     private List<Sprite> boxSpritesList = new ArrayList<Sprite>();
  39.  
  40.     @Override
  41.     public Engine onLoadEngine() {
  42.         Toast.makeText(this, "Pong game", Toast.LENGTH_LONG).show();
  43.         this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  44.         return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
  45.                 new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
  46.                 this.mCamera));
  47.     }
  48.  
  49.     @Override
  50.     public void onLoadResources() {
  51.         this.mTexture = new Texture(256, 32,
  52.                 TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  53.         this.mPongTextureRegion = TextureRegionFactory.createFromAsset(
  54.                 this.mTexture, this, "gfx/pong.png", 0, 0);
  55.         this.mBallTextureRegion = TextureRegionFactory.createTiledFromAsset(
  56.                 this.mTexture, this, "gfx/ball.png", 97, 0, 1, 1);
  57.         this.mBoxTextureRegion = TextureRegionFactory.createFromAsset(
  58.                 this.mTexture, this, "gfx/box.png", 114, 0);
  59.  
  60.         this.mEngine.getTextureManager().loadTexture(this.mTexture);
  61.     }
  62.  
  63.     @Override
  64.     public Scene onLoadScene() {
  65.         this.mEngine.registerUpdateHandler(new FPSLogger());
  66.  
  67.         final Scene scene = new Scene(1);
  68.         scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
  69.  
  70.         final int centerX = (CAMERA_WIDTH - this.mPongTextureRegion.getWidth()) / 2;
  71.         final int centerY = CAMERA_HEIGHT - this.mPongTextureRegion.getHeight();
  72.         final Sprite pong = new Sprite(centerX, centerY,
  73.                 this.mPongTextureRegion) {
  74.             @Override
  75.             public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
  76.                     final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  77.                 this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
  78.                         centerY);
  79.                 return true;
  80.             }
  81.         };
  82.  
  83.         final Ball ball = new Ball(centerX, 50, this.mBallTextureRegion);
  84.         ball.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY);
  85.  
  86.         pong.setScale(1);
  87.         scene.getTopLayer().addEntity(pong);
  88.         scene.getTopLayer().addEntity(ball);
  89.  
  90.         int startX = 10;
  91.  
  92.         for (int i = 0; i < Math.floor(CAMERA_WIDTH / 32); i++) {
  93.             Sprite boxSprite = new Sprite(startX, 10, this.mBoxTextureRegion) {
  94.  
  95.             };
  96.             boxSpritesList.add(boxSprite);
  97.             startX += 32;
  98.         }
  99.  
  100.         for (Sprite boxSprite : boxSpritesList) {
  101.             boxSprite.setScale(1);
  102.             scene.getTopLayer().addEntity(boxSprite);
  103.         }
  104.  
  105.         scene.registerTouchArea(pong);
  106.         scene.setTouchAreaBindingEnabled(true);
  107.  
  108.         scene.registerUpdateHandler(new IUpdateHandler() {
  109.  
  110.             @Override
  111.             public void reset() {
  112.             }
  113.  
  114.             @Override
  115.             public void onUpdate(final float pSecondsElapsed) {
  116.                 if (pong.collidesWith(ball)) {
  117.                     ball.setVelocityY(-DEMO_VELOCITY);
  118.                 }
  119.                 final List<Sprite> toRemoved = new ArrayList<Sprite>();
  120.                 for (Sprite box : boxSpritesList) {
  121.                     if (ball.collidesWith(box)) {
  122.                         toRemoved.add(box);
  123.                         ball.setVelocityY(DEMO_VELOCITY);
  124.                         break;
  125.                     }
  126.                 }
  127.  
  128.                 Game.this.runOnUpdateThread(new Runnable() {
  129.  
  130.                     @Override
  131.                     public void run() {
  132.                         for (Sprite sprite : toRemoved) {
  133.                             scene.getTopLayer().removeEntity(sprite);
  134.                             boxSpritesList.remove(sprite);
  135.                         }
  136.                     }
  137.                 });
  138.             }
  139.         });
  140.  
  141.         return scene;
  142.     }
  143.  
  144.     @Override
  145.     public void onLoadComplete() {
  146.  
  147.     }
  148.  
  149.     private static class Ball extends AnimatedSprite {
  150.         public Ball(final float pX, final float pY,
  151.                 final TiledTextureRegion pTextureRegion) {
  152.             super(pX, pY, pTextureRegion);
  153.         }
  154.  
  155.         @Override
  156.         protected void onManagedUpdate(final float pSecondsElapsed) {
  157.             if (this.mX < 0) {
  158.                 this.setVelocityX(DEMO_VELOCITY);
  159.             } else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
  160.                 this.setVelocityX(-DEMO_VELOCITY);
  161.             }
  162.  
  163.             if (this.mY < 0) {
  164.                 this.setVelocityY(DEMO_VELOCITY);
  165.             } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
  166.                 this.setVelocityY(-DEMO_VELOCITY);
  167.             }
  168.  
  169.             super.onManagedUpdate(pSecondsElapsed);
  170.         }
  171.     }
  172. }
  173.  
Parsed in 0.060 seconds, using GeSHi 1.0.8.4
Axeves
Freshman
Freshman
 
Posts: 8
Joined: Tue Dec 28, 2010 10:10 pm

Top

Re: Andengine problems

Postby Axeves » Sat Jan 01, 2011 2:53 am

And yes, it's the arkanoid tutorial posted here:

http://droidcake.com/2010/12/30/how-to- ... n-android/
Axeves
Freshman
Freshman
 
Posts: 8
Joined: Tue Dec 28, 2010 10:10 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 5 guests