I am new to andengine and want to know that how I can switch between two BaseGameActivities. And also when switching from first activity to second, there is no black screen transition in between switching. Is there any possible way to do it.
Please help me out.
A BaseGameActivity can be used as any other Android Activity, too:
Intent intent = new Intent(this, MyOtherBaseGameActivity.class);
startActivity(intent);
So if you want to change from your program to another app (maybe by opening the browser…) you can do that as with any other Android App, too. However if both Activities are part of your own App, there is rarely a case where this is recommendable (It is like starting a second program). Although it is possible to exchange data between the activities as described in this post.
But maybe you are only looking for a way to switch between Views in AndEngine. If that's the case you can switch between Scenes without any transition necessary.
MyOtherScene secondScene = new MyOtherScene();
mEngine.setScene(secondScene);
That way you can switch between what is being displayed, without needing to load every image again.
EDIT:
Since you can't use AndEngine to switch between Activities, nor is a smooth switching between scenes possible. Here a quick example on how to switch between two screens (e.g. menus). In this example the screens are actually 2 different images (as big as the display … maybe some background images). Note: there is no such thing as 'screens' in AndEngine, it is simply a self made class that extends Entity.
Your Screen
public MyScreen extends Entity{
private float firstX;
public MyScreen(Sprite niceBackgroundImage1, Sprite niceBackgroundImage2){
this.attachChild(niceBackgroundImage1); // attach something to the screen, so you can see it (preferably an image that is as big as your camera)
this.attachChild(niceBackgroundImage2);
this.firstY=-1; // this is a variable to remember the first x coordinate touched
}
#Override
public boolean onAreaTouched(TouchEvent sceneTouchEvent, float touchAreaLocalX, float touchAreaLocalY) {
if(sceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN){
this.firstY=touchAreaLocalX; // remember the x, on the first touch
}
if(sceneTouchEvent.getAction()==TouchEvent.ACTION_MOVE){
if(touchAreaLocalX>firstY+20){
// user swiped from left to right (at least 20 pixels)
niceBackgroundImage1.registerEntityModifier(new MoveModifier(3f, 0, niceBackgroundImage1.getWidth(), 0, 0, EaseBounceOut.getInstance()));
// the last line actualy moves the nice..image1 from left to right (in 3 seconds) and lets it bounce a little bit before it is completely out of the screen
return true;
}
}
return false;
}
...
}
Your Activity
private HUD hud; // create a HUD
...
#Override
protected void onCreateResources() {
this.hud = new HUD(); // init the HUD
this.myScreen = new MyScreen(image1, image2) // init the screen
this.hud.attachChild(myScreen); // attach the screen to the hud
mEngine.getCamera().setHud(hud); // attach your HUD to the camera
}
#Override
protected Scene onCreateScene() {
Scene myScene = new Scene();
myScene.registerTouchArea(myScreen); // you have to register the touch area of your Screen Class to the scene.
mEngine.setScene(myScene);
}
And this is how it works:
you create yourself a own screen class that extends Entity. An Entity can be everything visible in AndEngine (like a Sprite, Rectangle or even a whole scene). Put something in your screen class to make it look nice, preferably a big image that fills the whole display. That image will be responsible to register the touch afterwards. If the image is too small and the user misses the image, then no touch will be registered.
In this case I attach the instance of MyScreen to the cameras HUD. That way it will be at a fixed position on the display and it will have a fixed size (just in case you want to make the scene scrollable or zoomable).
Now when the app starts the HUD will be created and attached to the camera and with it your MyScreen class. Then the scene will be created and the screen's area will be registered as touch area to the scene. When a swipe movement on a horizontal axis gets noticed by the screen class, the first image will move outside the screen (in the same direction as the swipe).
But be careful, this is just an example. There is nothing defined on how the touch has to act when the first image was moved outside the screen or how big the screen actually is etc...
I know this is quite a long example, maybe it won't even work the first time and it is definitely not the only way on how switching between different screens can be done. But it shows you how to override the onAreaTouched() method and register the entity modifier to make the image move. Hopefully it will lead you in the right direction, to accomplish what you want to do.
Related
I'm building a tank game using AndEngine that has multiple levels, but I'm not using multiple scenes, I'm sticking to 1 Main Game Scene that should reset and modify itself when the user beats the level.
I'm able to successfully modify the scene, but I'm having an issue with removing the enemies. There are multiple instances of an enemy sprite that the user has to kill, but when the user successfully completes the requirement to advance a level(killing x number of enemies), the enemies aren't reset; the instances from the previous level haven't been removed from the screen.
As a result, when a user is on Level 2, there might still be 3 or 4 enemies roaming around from Level 1 that the user didn't need to kill.
I tried using detachChild to remove the enemy from the screen and attachChild to instantly add them back, but when the next level started, the enemies wouldn't spawn.
How can I remove all instances of the enemy sprite that are currently on the screen without affecting the spawning?
when you are starting the game you have to create different layers(Entities) like gameLayer,
background Layer, HUD Layer... .So that you can update the items based on the situation.
This process make you unload resources smoothly when level is completed.
Coming to your requirement ... add every sprite instance to array list when it is created.
Remove all these as follows
public static void removeSprites(List<Sprite> spriteList, IEntity scene){
for(int i = spriteList.size() - 1; i >= 0; i--) {
final Sprite sprite = spriteList.get(i);
scene.detachChild(sprite);
spriteList.remove(i);
}
spriteList = null;
System.gc();
}
you must also unload Texture Atlases in your game
Well, try to use this method: sprite.detachchildren()
I have an interesting problem with displaying two or quads at the same time while displaying one is working fine.
I was able to implement this popular tutorial for displaying a simple quad with texture:
http://www.jayway.com/2010/02/15/opengl-es-tutorial-for-android-part-v/
So basically i have a class named SimplePlane that extends Mesh class exactly as in tutorial.
I create an instance of SimplePlane:
public void onSurfaceCreated(...){
plane1 = new SimplePane(1,1);
plane2 = new SimplePane(1,1);
plane1.z = 2.0f
plane2.z = 3.0f
}
and then I draw mesh:
public void draw(GL10 gl) {
//set all gl variables as usual for opengl
plane1.draw(gl); // is displayed properly
plane2.draw(gl); //for some reason is not visible even that its behind plane1 and bigger to make sure plane1 is not covering plane2
}
The problem is only the first plane1 is being displayed.
If in my code i place plane2 first then plane 2 is diplayed and plane1 is not.
Mind you its not the z position issue as i ruled it out by creating one larger and semi transparent. And if i comment one out then the other is visible.
I added logging and both plane's draw methods are being called but only one is visible.
Am I allowed to take this approach calling one draw(gl) after another or i have to create a group object as in tutorial?
I am developing my first app in Andengine for the first time.Description of my project is:
4 pieces are placed near to each other
move and place 4 pieces to 4 different plates
on a successful placement of 4 different pieces to 4 different plates, I generate 4 new pieces and then step(1) ,(2) and (3) continue.
Now I have implemented this scenario like this:
At initial i made new BitmapTextureAtlas1 and sprites1 and allocated specifics position
for new Object i made new BitmapTextureAtlas and assigned to previous BitmapTextureAtlas1 and similar to sprites
3.I registerToucharea and atachchild and then unregisterTouchArea and DeatachChild and Follow(1 and 2 and 3)
Is this right Approach to do It.
The problem I face is this:
When i place pices1 To any plate ,It moves Successfully but their is some thing hidden at initial place of pices1 that when i again move from initial place of pice1 to any plate ..my flag increment that some object is moved
From #GulZar question:
Is this right Approach to do It?
Answer: No. You always load the resources(Graphics) before starting game play & it should only once.After finished the use of sprite you can call BitmapTextureAtlas.unload();
May be you think: Every time detaching a Sprite, You need to load Graphics again. Detaching a sprite is only cause to remove it from the scene, not from the memory.
You can create custom sprite class to use frankly. Example:
public class Food extends PixelPerfectAnimatedSprite {
public Food(float pX, float pY,
PixelPerfectTiledTextureRegion pTiledTextureRegion,
VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
}
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
return true;
}
#Override
public void onAttached() {
super.onAttached();
}
}
Here, I extend PixelPerfectAnimatedSprite for pixel perfect collision. You can use Sprite or AnimatedSprite. Then you can override onAreaTouched,onAttached() and all overrides method from Entity. Now, Inside onAreaTouched() you write logic what happens in moving.
After Doing above, you can call it From your Game play Scene. When a player moves one pieces to a plate, then set a condition/Flag either new pieces would be attached or not. Depends on your logic. Explain more to get a good tweet.
I know this has been asked before and I went through all the answers and nope this ain't that, and yes I have the activity named in the manifest so it's not that.
Here's my problem:
I have a sprite moving across the screen and when in the window where you choose the livewallpaper, I have a preference that lets the user choose the height (so they can move the sprite up or down) the only problem is the sprite is constantly moving so when they use the seekbar to move the sprite up or down I get the old 'Window already focused, ignoring focus gain of" Now if I go out and come back in again of course this stop and restarts it and the new preference hight is already in their so it moves to the new height then but not in the live preview.
Here is the sprite code:
private void addShipOne() {
int heightPosition = (screenSized / 2) - (screenSized / /*this.bigShipHeight*/gettheFreakingHeight());
int widthPosition = 0;
Point startPoint = new Point(widthPosition, heightPosition);
this._shipone.add(new SpaceShipOne(this._context, this, startPoint, 125));
(sorry about the method name I was using to test a quick version I was a bit frustrated at that point) lol
It then goes into an OnDraw method then into the render to render it all.
public void render(){
Canvas canvas = null;
try{
canvas = this._surfaceHolder.lockCanvas(null);
synchronized (this._surfaceHolder) {
this.onDraw(canvas);
}
}finally{
if(canvas != null){
this._surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
I have another preference that allows the user to choose the frequency of the ships appearing it works fine but it goes from left to right and changes ships then so it updates the frequency then, but the height one doesn't work, I tested the log to make sure the height int goes through and it does so its not that, the only thing I can come up with is the maybe add this when the ship turns left or right, but it does not work while its moving by the looks of it.
this.bigShipHeight is where the new height value goes into ( I was just testing it another way with the gettheFreakingHeight() (I used an if else statement for that method that used the frquency preference instead which does work nicely.
Just to add the full code part.
In the onDraw(Canvas canvas)
I render the _shipone as such
for (Renderable renderable : this._shipone) {
renderable.render(canvas);
}
Any help would be appreciated.
Thanks in advance
Sam
Ok I should of looked into the my sprite class first, sorry about this I answered my own question once again and it was too obvious, that's what I get for looking at the code way to long, I should walk away then come back it always is clearer then.
Just in case others run into this issue I'll put the answer.
In my sprite class which SpaceShipOne runs from I just added this
this.getSprite().setYPos(this._blimp.gettheFreakingHeight());
This updates right away with no window already focused.
I don't know why I didn't see this last night because it is the only place it can be added and be updated right away, all I had to do was set the Y position to the preference instead.
It works like a charm.
Hope this helps someone else out
Sam
I have created a little screenmanager (to handle multiple scenes), where every class extends from a custom class called Screen, and does the following (for example) in its load method:
public Scene load() {
BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
SceneManager.loadTexture(mBitmapTextureAtlas);
scene.attachChild(bgSprite);
return scene;
}
The problem is that sometimes, if you move fast among screens, some sprites are not being rendered, sometimes they are (it depends on how fast I switch between scenes).
I guess the problem might be that I'm attaching the sprites to the scene when they still have been not fully loaded in memory. Can it be? Any idea how to solve this problem?
Yes it happens if you move across scenes, so you can set boolean flags for sprites. if true then perform operations. It is specially useful when perform collisionDetections.