Related
I am new to Unity, but not to C#, so I would love to avoid using Bolt. I don't mind spending the extra time making sure that my animations are correct.
I am trying to get a gun to shoot (the animation part [aka recoil])
As you probably can see that there are no parameters (in the first image). In the parameter box (where it says list is empty in the animator window), I clicked on the '+' to create a new parameter trigger (called it "M1911SHOOT")
However it does resolve the Parameter does not exist error message but nothing happens afterwards
Here is my code
M1911.cs
public class M1911 : Weapons
{
private const string IDLE = "M1911IDLE";
private const string BASIC = "M1911SHOOT";
// Start is called before the first frame update
void Start()
{
IDLEANIM = IDLE;
BASICATKANIM = BASIC;
weaponName = "Weapon";
damage = 15;
heavyDamage = 35;
weight = 5;
staminaCost = 5;
range = 3.5f;
atkDelay = .7f;
type = WEAPONTYPE.MELEE;
Init();
}
}
Weapons.cs
public class Weapons : MonoBehaviour
{
protected enum WEAPONTYPE
{
NONE,
RANGED,
MELEE
}
protected string weaponName;
protected int damage;
protected int heavyDamage;
protected int weight;
protected int staminaCost;
protected float range;
protected WEAPONTYPE type;
protected bool isAtking;
protected float atkDelay;
protected string BASICATKANIM;
protected string HEAVYATKANIM;
protected string IDLEANIM;
protected string CURRENTANIMATION;
private Animator _animator;
protected void Init()
{
CURRENTANIMATION = IDLEANIM;
_animator = GetComponent<Animator>();
isAtking = false;
}
public int Attack()
{
if (!isAtking)
{
isAtking = true;
ChangeAnimation(BASICATKANIM);
return damage;
}
return 0;
}
void AttackComplete()
{
isAtking = false;
}
public void ChangeAnimation(string newAnimationState)
{
if(newAnimationState == CURRENTANIMATION)
return;
if (isAtking)
{
CURRENTANIMATION = newAnimationState;
Invoke("AttackComplete", atkDelay);
}
_animator.SetBool(BASICATKANIM, true);
_animator.SetBool(IDLEANIM, false);
CURRENTANIMATION = newAnimationState;
}
}
Player.cs
public class Player : CharacterAttributes
{
public Weapons weapon;
// Start is called before the first frame update
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
Move();
if (Input.GetMouseButtonDown(0))
Attack();
}
void Move()
{
//Movement stuff
}
void Attack()
{
weapon.Attack();
}
}
Wrong method in Weapon.cs
Needed
_animator.Play(BASICATKANIM);
instead of
_animator.SetBool(IDLEANIM, false);
I wanted to play an animation, not play around with the parameters for the animator
enter image description hereI have 4 sprites. I am placing all 4 sprites in different place on the mobile screen. But it is taking the last added sprite image that is (sprite4.png) all the sprites images(sprite1.png, sprite2.png, sprite3.png and sprite4.png).
In short its displaying all the sprite images same.
The code is given below, please share your experience
// the below code is running without error but displaying same images in all the sprites
package com.example.test1;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
/*;
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga
*
* #author Nicolas Gramlich
* #since 15:13:46 - 15.06.2010
*/
public class Puzzle extends GameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
final Scene scene = new Scene();
// ===========================================================
// Fields
// ===========================================================
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion spriteTextureRegion1;
private ITextureRegion spriteTextureRegion2;
private ITextureRegion spriteTextureRegion3;
private ITextureRegion spriteTextureRegion4;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
#Override
public EngineOptions onCreateEngineOptions() {
// Toast.makeText(this, "Touch & Drag the face!", Toast.LENGTH_LONG).show();
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 96, 96, TextureOptions.BILINEAR);
this.spriteTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite1.png", 0, 0);
this.spriteTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite2.png", 0, 0);
this.spriteTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite3.png", 0, 0);
this.spriteTextureRegion4= BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite4.png", 0, 0);
// scene.attachChild(sprite1);
this.mBitmapTextureAtlas.load();
}
#Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
/* set sprite1 positions on screen */
final float sprite1_posX = (40);
final float sprite1_posY = (40);
final Sprite sprite1 = new Sprite(sprite1_posX, sprite1_posY, this.spriteTextureRegion1, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
//sprite1.setScale(3);
scene.attachChild(sprite1);
scene.registerTouchArea(sprite1);
scene.setTouchAreaBindingOnActionDownEnabled(true);
/* set sprite2 positions on screen */
final Sprite sprite2 = new Sprite(sprite1_posX, sprite1_posY + (sprite1.getHeight()*3 + 10), this.spriteTextureRegion2, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
//sprite2.setScale(3);
scene.attachChild(sprite2);
scene.registerTouchArea(sprite2);
scene.setTouchAreaBindingOnActionDownEnabled(true);
/* set sprite3 positions on screen */
final Sprite sprite3 = new Sprite(sprite1_posX, sprite1_posY + (sprite1.getHeight() + 20), this.spriteTextureRegion3, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
// sprite3.setScale(3);
scene.attachChild(sprite3);
scene.registerTouchArea(sprite3);
scene.setTouchAreaBindingOnActionDownEnabled(true);
/* set sprite3 positions on screen */
final Sprite sprite4 = new Sprite(sprite1_posX, sprite1_posY + (sprite1.getHeight() + 30), this.spriteTextureRegion4, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
// sprite4.setScale(3);
scene.attachChild(sprite4);
scene.registerTouchArea(sprite4);
scene.setTouchAreaBindingOnActionDownEnabled(true);
return scene;
}
}
Oh sorry I know what is the problem:
this.spriteTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite1.png", 0, 0);
this.spriteTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite2.png", 0, 0);
this.spriteTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite3.png", 0, 0);
this.spriteTextureRegion4= BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite4.png", 0, 0);
At the end, where you put 0, 0 - you declare where your region should be located on the Texture atlas. Imagine stickers you put on the sheet of paper. You put one, then on this you put another, and third and fourth. This way you can see only the one that is on top. So you have to put your regions in different places of the texture atlas. If all your images are 40x40 it should look like this (also your atlas might be bigger (512x512):
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
this.spriteTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite1.png", 0, 0);
this.spriteTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite2.png", 50, 0);
this.spriteTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite3.png", 0, 50);
this.spriteTextureRegion4= BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sprite4.png", 50, 50);
I am trying to make a HUD at AndEngine, but when I write this code below and run the emulator, only thing appears on Emulator is a black screen with crashed HUD image.
This is what I want to display,
and This is how my code show.
This is my code -
package com.example.andenginetester;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
public class SpriteTester extends SimpleBaseGameActivity {
public static final int CAMERA_WIDTH = 720;
public static final int CAMERA_HEIGHT = 480;
private HUD myHUD;
private Sprite myHUDSprite;
private BitmapTextureAtlas myAtlas;
private TextureRegion myHUDImage;
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
#Override
public EngineOptions onCreateEngineOptions() {
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
protected void onCreateResources() {
myAtlas = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
myHUDImage = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.myAtlas, this, "gfx/HUD.png", 0, 0);
mEngine.getTextureManager().loadTexture(myAtlas);
}
#Override
protected Scene onCreateScene() {
final Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
myHUDSprite = new Sprite(0, 0, myHUDImage, this.getVertexBufferObjectManager());
myHUD.attachChild(myHUDSprite);
camera.setHUD(myHUD);
return scene;
}
}
How I should fix it?
Well, I managed to get the correct result , let me know if it works for you:
a) I updated the AndroidManifest.xml
Added in the <activity ... >:
1) android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|mcc|mnc"
2) android:screenOrientation="landscape"
b) The updated activity:
public class MainActivity extends SimpleBaseGameActivity {
public static final int CAMERA_WIDTH = 720;
public static final int CAMERA_HEIGHT = 480;
private HUD myHUD = new HUD();
private Sprite myHUDSprite;
private BitmapTextureAtlas myAtlas;
private TextureRegion myHUDImage;
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
#Override
public EngineOptions onCreateEngineOptions() {
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
protected void onCreateResources() {
myAtlas = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
myHUDImage = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
this.myAtlas, this, "gfx/HUD.png", 0, 0);
mEngine.getTextureManager().loadTexture(myAtlas);
}
#Override
protected Scene onCreateScene() {
final Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
myHUDSprite = new Sprite(0, 0, myHUDImage,
this.getVertexBufferObjectManager());
myHUD.attachChild(myHUDSprite);
myHUD.setPosition(CAMERA_WIDTH/2,CAMERA_HEIGHT/2);
camera.setHUD(myHUD);
return scene;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm developing a 2D car game for my University project.
I have developed it up to the point that user's car can move and traffic cars come from above. But I have no clue about how to destroy the user's car when it collied with a traffic car. Can anyone tell how to detect collision and after that how to destroy it.
public class MainActivity extends BaseGameActivity{
Scene scene; // declare a scene object
protected static final float CAMERA_WIDTH = 800; // define camera width
protected static final float CAMERA_HEIGHT = 520; //define camera height
/*----- background -----------*/
BitmapTextureAtlas backbitmapTextureAtlas; // declare a bitmap texture
ITextureRegion backiTextureRegion; // declare a i texture region to hold image
Sprite backsPlayer; // sprite to display the image
PhysicsWorld backpWorld;
SensorManager backsensor;
Vector2 backvec;
ITexture backparallax_background;
protected VertexBufferObjectManager backvbom;
org.andengine.engine.camera.Camera camera;
/*----- /background -----------*/
/*----user's car---------*/
BitmapTextureAtlas bitmapTextureAtlas;
ITextureRegion iTextureRegion;
Vector2 vec;
PhysicsWorld pWorld;
SensorManager sensor;
Sprite sPlayer;
/*----/user's car---------*/
/*------ traffic cars----------*/
BitmapTextureAtlas bitmapTextureAtlasTraffic1;
ITextureRegion iTextureRegionTraffic1;
Sprite sPlayerTraffic1;
BitmapTextureAtlas bitmapTextureAtlasTraffic2;
ITextureRegion iTextureRegionTraffic2;
Sprite sPlayerTraffic2;
BitmapTextureAtlas bitmapTextureAtlasTraffic3;
ITextureRegion iTextureRegionTraffic3;
Sprite sPlayerTraffic3;
BitmapTextureAtlas bitmapTextureAtlasTraffic4;
ITextureRegion iTextureRegionTraffic4;
Sprite sPlayerTraffic4;
MoveXModifier mod1;
MoveXModifier mod2;
MoveXModifier mod3;
MoveXModifier mod4;
/*------ /traffic cars----------*/
#Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
camera = new org.andengine.engine.camera.Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT); // create camera
EngineOptions options= new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT), camera); //create engine options
return options;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// TODO Auto-generated method stub
/* ---------------parallax back code------------------*/
backparallax_background = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "gfx/back2.png");
backiTextureRegion = TextureRegionFactory.extractFromTexture(this.backparallax_background);
this.backparallax_background.load();
/* ---------------/parallax back code------------------*/
loadGfx(); // load user's car
loadTraffic();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
/*--------load traffic cars------------*/
private void loadTraffic() {
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // give the path of image folder
bitmapTextureAtlasTraffic1 = new BitmapTextureAtlas(getTextureManager(), 256, 256);// create a bit map to hold the picture and give size according to the image
iTextureRegionTraffic1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlasTraffic1, this, "traffic1.png", 0,0);
bitmapTextureAtlasTraffic1.load();
//----- traffic 2--------------
bitmapTextureAtlasTraffic2 = new BitmapTextureAtlas(getTextureManager(), 256, 256);// create a bit map to hold the picture and give size according to the image
iTextureRegionTraffic2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlasTraffic2, this, "traffic2.png", 0,0);
bitmapTextureAtlasTraffic2.load();
//----- traffic 3--------------
bitmapTextureAtlasTraffic3 = new BitmapTextureAtlas(getTextureManager(), 256, 256);// create a bit map to hold the picture and give size according to the image
iTextureRegionTraffic3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlasTraffic3, this, "traffic3.png", 0,0);
bitmapTextureAtlasTraffic3.load();
//----- traffic 4--------------
bitmapTextureAtlasTraffic4 = new BitmapTextureAtlas(getTextureManager(), 256, 256);// create a bit map to hold the picture and give size according to the image
iTextureRegionTraffic4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlasTraffic4, this, "traffic4.png", 0,0);
bitmapTextureAtlasTraffic4.load();
}
/*--------load user's car------------*/
private void loadGfx() {
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // give the path of image folder
bitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 256, 256);// create a bit map to hold the picture and give size according to the image
iTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas, this, "usercar.png", 0,0);
bitmapTextureAtlas.load();
}
/*--------load user's car------------*/
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// TODO Auto-generated method stub
scene = new Scene(); // create the object of scene
/*------ parallax background---------*/
final AutoParallaxBackground auto_background = new AutoParallaxBackground(0, 0, 0, 200);
final Sprite background_sprite = new Sprite(0,0, this.backiTextureRegion,backvbom);
auto_background.attachParallaxEntity(new ParallaxEntity(1.7f,background_sprite));
scene.setBackground(auto_background);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
// TODO Auto-generated method stub
// set traffic car1
sPlayerTraffic1 = new Sprite(10,350,iTextureRegionTraffic1,this.mEngine.getVertexBufferObjectManager());
sPlayerTraffic2 = new Sprite(300,280,iTextureRegionTraffic2,this.mEngine.getVertexBufferObjectManager());
sPlayerTraffic3 = new Sprite(400,190,iTextureRegionTraffic3,this.mEngine.getVertexBufferObjectManager());
sPlayerTraffic4 = new Sprite(50,70,iTextureRegionTraffic4,this.mEngine.getVertexBufferObjectManager());
mod1=new MoveXModifier(5,-600,800){
#Override
protected void onModifierFinished(IEntity pItem) {
// TODO Auto-generated method stub
Random r = new Random();
int y = r.nextInt(370-350)+350;// set y randomly
int speed = r.nextInt(3-2)+3; // set speed randomly
sPlayerTraffic1.setY(y); // set y
int x = r.nextInt(800-500)+200; // set x randomly
x = -x;
mod1.reset(speed, x, 800);
super.onModifierFinished(pItem);
}
};// moving down the traffic1 car
mod2=new MoveXModifier(4,200,800){
#Override
protected void onModifierFinished(IEntity pItem) {
// TODO Auto-generated method stub
Random r = new Random();
int y = r.nextInt(300-285)+285; // set y randomly
int speed = r.nextInt(5-3)+3; // set speed randomly
sPlayerTraffic2.setY(y); // set y
int x = r.nextInt(600-200)+200; // set x randomly
x = -x;
mod2.reset(speed, x, 800);
super.onModifierFinished(pItem);
}
};// moving down the traffic2 car
mod3=new MoveXModifier(3,-600,800){
#Override
protected void onModifierFinished(IEntity pItem) {
// TODO Auto-generated method stub
Random r = new Random();
int y = r.nextInt(190-150)+150;
int speed = r.nextInt(3-2)+2;
if(speed == 2){
y = 150;
}
sPlayerTraffic3.setY(y);
int x = r.nextInt(2000-800)+800;
x = -x;
mod3.reset(speed, x, 800);
super.onModifierFinished(pItem);
}
};// moving down the traffic3 car
mod4=new MoveXModifier(3,50,800){
#Override
protected void onModifierFinished(IEntity pItem) {
// TODO Auto-generated method stub
Random r = new Random();
int y = r.nextInt(100-70)+70;
int speed = r.nextInt(3-2)+2;
sPlayerTraffic4.setY(y);
int x = r.nextInt(600-200)+200;
x = -x;
mod4.reset(speed, x, 800);
super.onModifierFinished(pItem);
}
};// moving down the traffic4 car
sPlayerTraffic1.registerEntityModifier(mod1);
sPlayerTraffic2.registerEntityModifier(mod2);
sPlayerTraffic3.registerEntityModifier(mod3);
sPlayerTraffic4.registerEntityModifier(mod4);
//now set the x,y coordination of the image to display the right position we want
sPlayer = new Sprite(500,350,iTextureRegion,this.mEngine.getVertexBufferObjectManager()){ // user's car x,y
// touch event for user's car
#Override
public boolean onAreaTouched(org.andengine.input.touch.TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
{
this.setPosition(500 , pSceneTouchEvent.getY());
//website code
this.setPosition(pSceneTouchEvent.getX(),
this.getY());
//Detects if player is outside of bounds
final float width = this.getWidth();
final float height = this.getHeight();
float x = pSceneTouchEvent.getX() - width / 2 ;
float y = pSceneTouchEvent.getY() - height / 2;
if (x < 0)
x = 0;
if (y < 65) // right side of the road
y = 65;
if (x > (CAMERA_WIDTH - width))
x = CAMERA_WIDTH - width;
if (y > (CAMERA_HEIGHT - height-70)) // left side of the road
y = (CAMERA_HEIGHT - height-70);
this.setPosition(500, y);
return true;
}
};
//touch ----------------------------------------------------------
scene.registerTouchArea(sPlayer);
//-----------------------------------------------------------------
this.scene.attachChild(sPlayer);
this.scene.attachChild(sPlayerTraffic1);
this.scene.attachChild(sPlayerTraffic2);
this.scene.attachChild(sPlayerTraffic3);
this.scene.attachChild(sPlayerTraffic4);
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
#Override
public void beginContact(Contact contact)
{
final Fixture x1 = contact.getFixtureA();
final Fixture x2 = contact.getFixtureB();
if(x1==PlayerBody && x2==EnemyBody){
DestroyMethod();
}
}
};
return contactListener;
}
YourPhysicsWorld.setContactListener(createContactListener());
private void DestroyMethod(){
YourPhysicsWorld.destroyBody(EnemyBody);
}
I am new in android game development,I have two buttons on screen , one for forward and one for reverse, also one player sprite.I want to move large background on buttons click.
I have idea about auto-parallax background,but how can i move background on buttons click?
please help
I am doing some thing like this, but it is not smooth and also i think this is not a good approach
public class MainActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener {
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
// ===========================================================
// Fields
// ===========================================================
BitmapTextureAtlas ForwardAtlas,backgroundAtlas,backAtlas,mFontTexture;
ITextureRegion ForwardAtlasRegion,backgroundAtlasRegion,backAtlasRegion;
Sprite ForwardSprite,backgroundSprite,backSprite;
AnimatedSprite player;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mPlayerTextureRegion;
SequenceEntityModifier modifier;
AutoParallaxBackground autoParallaxBackground;
boolean flag=true;
Scene scene;
private Font mFont;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,new FillResolutionPolicy(),camera);
return eo;
}
#Override
protected void onCreateResources() {
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR);
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
this.mBitmapTextureAtlas.load(this.getTextureManager());
ForwardAtlas = new BitmapTextureAtlas(60,60);
ForwardAtlas.load(getTextureManager());
ForwardAtlasRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ForwardAtlas, getApplicationContext(), "forward.png",0,0);
backAtlas = new BitmapTextureAtlas(60,60);
backAtlas.load(getTextureManager());
backAtlasRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backAtlas, getApplicationContext(), "Back.png",0,0);
backgroundAtlas = new BitmapTextureAtlas(1000,480);
backgroundAtlas.load(getTextureManager());
backgroundAtlasRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundAtlas, getApplicationContext(), "background-2.png",0,0);
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
this.getFontManager().loadFont(this.mFont);
}
#Override
protected Scene onCreateScene() {
// TODO Auto-generated method stub
this.mEngine.registerUpdateHandler(new FPSLogger());
scene = new Scene();
final int playerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
final int playerY = CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight() - 5;
//final Text x = new Text(100, 60, this.mFont,Integer.toString(playerX) , HorizontalAlign.CENTER);
//final Text y = new Text(120, 90, this.mFont,Integer.toString(playerY) , HorizontalAlign.CENTER);
player = new AnimatedSprite(playerX, playerY, this.mPlayerTextureRegion);
player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());
player.setScale(2);
player.animate(new long[]{200, 200, 200}, 6, 8, true);
ForwardSprite = new Sprite(390,250, ForwardAtlasRegion);
backSprite = new Sprite(40,250, backAtlasRegion);
backgroundSprite = new Sprite(0,0,backgroundAtlasRegion);
autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.backgroundAtlasRegion.getHeight(), this.backgroundAtlasRegion)));
scene.setBackground(autoParallaxBackground);
scene.attachChild(player);
scene.attachChild(ForwardSprite);
scene.registerTouchArea(ForwardSprite);
scene.attachChild(backSprite);
scene.registerTouchArea(backSprite);
scene.setOnAreaTouchListener(new IOnAreaTouchListener() {
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
ITouchArea pTouchArea, float pTouchAreaLocalX,
float pTouchAreaLocalY)
{
if(pSceneTouchEvent.isActionUp())
{
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - backgroundAtlasRegion.getHeight(), backgroundAtlasRegion)));
if(pTouchArea == ForwardSprite)
if(pTouchArea == backSprite)
return true;
}
if(pSceneTouchEvent.isActionMove()) {
if(pTouchArea == ForwardSprite)
{
player.animate(new long[]{200, 200, 200}, 3, 5, true);
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - backgroundAtlasRegion.getHeight(), backgroundAtlasRegion)));
//backgroundSprite.setPosition(backgroundSprite.getX()-5, backgroundSprite.getY());
}
if(pTouchArea == backSprite)
{
player.animate(new long[]{200, 200, 200}, 9, 11, true);
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(10.0f, new Sprite(0, CAMERA_HEIGHT - backgroundAtlasRegion.getHeight(), backgroundAtlasRegion)));
//backgroundSprite.setPosition(backgroundSprite.getX()+5, backgroundSprite.getY());
}
return true;
}
return false;
}
});
//scene.setOnSceneTouchListener(this);
return scene;
}
I've just answered similiar question. Please take a look at stop and start auto parallaxbackground in andengine
There isn't such function provided in andengine so you will need extend or add some features in AutoParallaxBacground class of AndEngine library.
package org.andengine.entity.scene.background;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* #author Nicolas Gramlich
* #since 19:44:31 - 19.07.2010
*/
public class AutoParallaxBackground extends ParallaxBackground {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private float mParallaxChangePerSecond;
private boolean movingForward;
private boolean movingBackward;
// ===========================================================
// Constructors
// ===========================================================
public AutoParallaxBackground(final float pRed, final float pGreen, final float pBlue, final float pParallaxChangePerSecond) {
super(pRed, pGreen, pBlue);
this.mParallaxChangePerSecond = pParallaxChangePerSecond;
}
// ===========================================================
// Getter & Setter
// ===========================================================
public void setParallaxChangePerSecond(final float pParallaxChangePerSecond) {
this.mParallaxChangePerSecond = pParallaxChangePerSecond;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
#Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
if(movingForward)
this.mParallaxValue += this.mParallaxChangePerSecond * pSecondsElapsed;
else if (moving Backward)
this.mParallaxValue -= this.mParallaxChangePerSecond * pSecondsElapsed;
}
// ===========================================================
// Methods
// ===========================================================
public void startForward(){
this.movingBackward = false;
this.movingForward = true;
}
public void startBackward(){
this.movingForward = false;
this.movingBackward = true;
}
public void stop(){
this.movingBackward = false;
this.movingForward = false;
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
I haven't tested that solution but it should work as expected. The only thing I am not sure right now is changing incrementation to decrementation enough.