libGDX 1.4.1 can't slow down animations - libgdx
This is what I have:
...
private Array<Sprite> fireballFadeOutAnim;
private Array<Sprite> bucketFadeOutAnim;
private TextureAtlas fballAnimSheet;
private TextureAtlas bucketAnimSheet;
private Animation catch_animation;
private Animation visual_feedback;
private float elapsedTime = 0f;
...
#Override
public void create () {
...
fballAnimSheet = new TextureAtlas("animations/fireball_fadeout.txt");
bucketAnimSheet = new TextureAtlas("animations/bucket_fadeout.txt");
fireballFadeOutAnim = fballAnimSheet.createSprites("fireball_fadeout");
bucketFadeOutAnim = bucketAnimSheet.createSprites("bucket_fadeout");
catch_animation = new Animation(1/15f, fballAnimSheet.getRegions(), Animation.PlayMode.NORMAL);
visual_feedback = new Animation(1/15f, bucketAnimSheet.getRegions(), Animation.PlayMode.LOOP);
...
}
#Override
public void render () {
...
// Set the anim_elipsed_time
elapsedTime += Gdx.graphics.getDeltaTime();
...
// I try to draw them later on (under the render method) like this:
// Start and draw animation of fireball catch
batch.begin();
batch.draw(catch_animation.getKeyFrame(elapsedTime, true), fireBallRect.getX(), fireBallRect.getY());
batch.end();
}
I can see both animations happening extremely fast (they look like a white-flash), they do not loop even if the animation mode is set to "LOOP".
What am I doing wrong ? I can't seem to be able to slow them down or to have them LOOP. Thanks much.
Edited:
As requested, I upload all the code
public class MainActivity extends ApplicationAdapter {
public enum State {
PAUSE,
RUN,
RESUME,
STOPPED
}
public static State state = State.RUN;
private SpriteBatch batch;
private OrthographicCamera camera;
private Stage stage;
private Texture backgroundImage;
private Texture bucketImage;
private Texture fireballImage;
private Texture heart;
private TextureRegion criticalTexture;
private Sprite backgroundSprit;
private Sprite bucketSprite;
private Sprite fireballSprite;
private Sprite heartSprite;
private Rectangle bucketRect;
private Rectangle fireBallRect;
private Rectangle bucketSpriteRect;
private ShapeRenderer bucketRenderer;
private ShapeRenderer fireballRenderer;
private Button pauseButton;
private Array<Rectangle> fireballArray;
private Skin progressBarSkin;
private long lastFireballTime;
public static Sound bucket_tap;
public static Sound bucket_drag;
private Sound fireball_missed_sound;
private Sound catchFireball;
private Sound pauseBtnSound;
private Music bgMusic;
private float volume = 0.30f; // music and sfx volume control
public static int w;
public static int h;
private int barLength = 325;
private int numberOfFails = 10; // change numberOfFails to increase or decrese
// the total num. of missed fireballs the player
private int deductedBarSegment = barLength / numberOfFails;
private int numOfBallsLeft = numberOfFails; // use this var to check how many fballs left
private long fireballFrequency = 1000000000L; // change fireballFrequency to increase or decrease
// the number of fireballs on the screen
private int fireballSpeed = 400; // change the fireballSpeed to increase or decrease
// the speed at which the fireballs fall
private InputProcessor gameInputProcessor; // this sets the game input controller for the bucket
private InputMultiplexer multiplexer; // this sets the input processors from the game and UI
/************************************ Animation Properties ************************************/
private TextureAtlas fballAtlas;
private TextureAtlas bucketAtlas;
private Animation catch_animation;
private Animation visual_feedback;
private float elapsedTime = 0f;
private Actor feedbackPanel;
/**********************************************************************************************/
#Override
public void create () {
// Parse Test
//ParseObject testObject = new ParseObject("TestObject");
//testObject.put("foo", "bar");
//testObject.saveInBackground();
// Catch the dynamic size of the screen based on the device that's ran on
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
// Set up the camera
camera = new OrthographicCamera();
camera.setToOrtho(false, (float)w, (float)h);
// Instantiate the batch
batch = new SpriteBatch();
/***************************** These renders are for debugging only *****************************/
bucketRenderer = new ShapeRenderer();
fireballRenderer = new ShapeRenderer();
/**************************************************************************************************/
/******************************************** Stage and HUD ***************************************/
stage = new Stage();
pauseButton = new Button(new TextureRegionDrawable(
new TextureRegion(new Texture(Gdx.files.internal("images/pase_button_red.png")))),
new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/pase_button_white.png")))));
pauseButton.setX(flipCoordinates(w, 150));
pauseButton.setY(flipCoordinates(h, 170));
pauseButton.act(Gdx.graphics.getDeltaTime());
stage.addActor(pauseButton);
pauseButton.addListener(new ChangeListener() {
#Override
public void changed (ChangeEvent event, Actor actor) {
// Do something when the pauseButton is pressed
// Control multiple touches on the pauseButton
if(state == State.RUN) {
state = State.PAUSE;
pauseBtnSound.play();
// Set the pauseButton icon to the white version
//pauseButton.setColor(Color.WHITE);
}
else {
state = State.RUN;
pauseBtnSound.play();
}
}
});
// Add the critical_life indicator to the stage as an actor
criticalTexture = new TextureRegion(new Texture(Gdx.files.internal("images/critical_life.png")));
feedbackPanel = new Actor() {
public void draw(Batch batch, float alpha) {
batch.draw(criticalTexture, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
getScaleX(), getScaleY(), getRotation());
}
};
feedbackPanel.setBounds(w / 2, h / 2,
criticalTexture.getRegionWidth(), criticalTexture.getRegionHeight());
// Set up an Action for the critical life actor
feedbackPanel.addAction(forever(sequence(scaleTo(2, 2, 0.5f), scaleTo(1, 1, 0.5f), delay(0.5f))));
feedbackPanel.act(Gdx.graphics.getDeltaTime());
stage.addActor(feedbackPanel);
/**************************************************************************************************/
/*********************************** Set up the Input Controllers *********************************/
// The multiplexer helps target several input processes (in this case, the stage and the
// InputProcessor.class)
multiplexer = new InputMultiplexer();
multiplexer.addProcessor(stage);
multiplexer.addProcessor(new GameInputProcessor());
// InputAdaptor
Gdx.input.setInputProcessor(multiplexer);
/**************************************************************************************************/
backgroundImage = new Texture("images/bg.jpg");
bucketImage = new Texture("images/bucket_small.png");
fireballImage = new Texture("images/fireball_small.png");
heart = new Texture("images/heart.png");
/*************************************** Animations Setup *****************************************/
fballAtlas = new TextureAtlas("animations/fireball_fadeout.txt");
bucketAtlas = new TextureAtlas("animations/bucket_fadeout.txt");
catch_animation = new Animation(0.50f, fballAtlas.getRegions());
visual_feedback = new Animation(0.15f, bucketAtlas.getRegions());
/**************************************************************************************************/
// Instantiate all sounds
bucket_tap = Gdx.audio.newSound(Gdx.files.internal("sounds/bucket_tap.ogg"));
bucket_drag = Gdx.audio.newSound(Gdx.files.internal("sounds/bucket_drag.ogg"));
fireball_missed_sound = Gdx.audio.newSound(Gdx.files.internal("sounds/fireball_missed.ogg"));
catchFireball = Gdx.audio.newSound(Gdx.files.internal("sounds/fastSteam.ogg"));
pauseBtnSound = Gdx.audio.newSound(Gdx.files.internal("sounds/pauseBtn_sound.ogg"));
bgMusic = Gdx.audio.newMusic(Gdx.files.internal("sounds/music_bg.mp3"));
bgMusic.setLooping(true);
bgMusic.play(); // <----------- turn bg music on/off
// Instantiate the progressBarSkin
progressBarSkin = new Skin();
progressBarSkin.add("progressBarBg", new Texture("images/progress-bar_bg.png"));
progressBarSkin.add("progressBar", new Texture("images/progress-bar_dynamic_red.png"));
// Create bg sprite
backgroundSprit = new Sprite(backgroundImage);
//backgroundSprit.setPosition((float)w / 2, (float)h / 2);
//backgroundSprit.setSize((float)w, (float)h);
// Bucket sprite
bucketSprite = new Sprite(bucketImage);
// Fireball sprite
fireballSprite = new Sprite(fireballImage);
// Heart sprite
heartSprite = new Sprite(heart);
// Set the bucketRect into the rect from the bucketSprite (THE GREEN ONE)
bucketSpriteRect = bucketSprite.getBoundingRectangle();
bucketSpriteRect.x = w / 2 - 100 / 2;
bucketSpriteRect.y = 20;
bucketSpriteRect.setSize(200, 200);
// Set the collision rectangle (THE RED ONE)
bucketRect = new Rectangle();
bucketRect.x = bucketSpriteRect.getX() + 70; // <---- adjust position left/right
bucketRect.y = bucketSpriteRect.getY() + 150; // <---- adjust position up/down
bucketRect.setSize(100, 10);
// Set a rectangle for each fireball sprite (THE WHITE ONE)
fireBallRect = new Rectangle();
fireBallRect.x = fireballSprite.getX();
fireBallRect.y = fireballSprite.getY();
fireBallRect.width = 55;
fireBallRect.height = 55;
fireballArray = new Array<Rectangle>();
}
#Override
public void render () {
// Devide the Render method into a few switch cases based on the game's state
switch (state) {
case RUN:
// Do all the batch drawing here
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
elipsedTime += Gdx.graphics.getDeltaTime();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundSprit, 0, 0, w, h);
batch.draw(bucketSprite, bucketSpriteRect.x, bucketSpriteRect.y,
bucketSprite.getWidth() - 160, bucketSprite.getHeight() - 140);
for(Rectangle fireball: fireballArray) {
batch.draw(fireballSprite, fireball.x, fireball.y,
fireballSprite.getWidth() - 20, fireballSprite.getHeight() - 20);
}
batch.draw(progressBarSkin.getSprite("progressBarBg"), 200, flipCoordinates(h, 170));
batch.draw(progressBarSkin.getSprite("progressBar"),
210, flipCoordinates(h, 162), barLength, 6); // <---- modify x, x, <x>, x to adjust the length of the life-bar
batch.draw(heartSprite, 95, flipCoordinates(h, 200), 90, 76);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
batch.end();
/***************************************Debugging Rectangles************************************/
// Draw rects around sprites
// bucketRenderer.begin(ShapeRenderer.ShapeType.Line);
// bucketRenderer.setColor(Color.GREEN);
// bucketRenderer.rect(bucketSpriteRect.getX(), bucketSpriteRect.getY(),
// bucketSprite.getWidth() - 160, bucketSprite.getHeight() -140);
// bucketRenderer.end();
//
// bucketRenderer.begin(ShapeRenderer.ShapeType.Line);
// bucketRenderer.setColor(Color.RED);
// bucketRenderer.rect(bucketRect.getX(), bucketRect.getY(), 100, 10);
// //bucketRenderer.point(bucketRect.getX(), bucketRect.getY(), 0f);
// bucketRenderer.end();
//
// fireballRenderer.begin(ShapeRenderer.ShapeType.Line);
// fireballRenderer.setColor(Color.WHITE);
// fireballRenderer.rect(fireBallRect.x, fireBallRect.y,
// fireballSprite.getWidth() - 20, fireballSprite.getHeight() - 20);
// fireballRenderer.end();
// To avoid sprite flickering, surround batch.draw with a game state check
if (state != State.PAUSE) {
Iterator<Rectangle> iter = fireballArray.iterator();
while(iter.hasNext()) {
fireBallRect = iter.next();
fireBallRect.y -= fireballSpeed * Gdx.graphics.getDeltaTime();
// Remove the fireBallRect from the array as soon as it leaves the screen
if(fireBallRect.y + 106 < 0) {
iter.remove();
// The player missed the fireball, play a the fireball_missed_sound
fireball_missed_sound.play();
// Subtract a ball from the total number allowed to be missed
numOfBallsLeft --;
// Call life deduction
deductLifePoints();
// Check to see if this is the last fball left to fail
if (numOfBallsLeft == 1) {
}
// Check to see if life points are 0 or < 0
if (numOfBallsLeft <= 0) {
// Game over logic
barLength = 0;
// Set the game state to PAUSED
//state = State.PAUSE;
//bucketSprite.setColor(Color.RED);
batch.begin();
batch.draw(bucketSprite, bucketSpriteRect.x, bucketSpriteRect.y,
bucketSprite.getWidth() - 160, bucketSprite.getHeight() - 140);
batch.end();
}
}
// Collision detector
if(bucketRect.overlaps(fireBallRect)) {
catchFireball.play(volume);
// Start and draw animagion of fireball catch
batch.begin();
batch.draw(catch_animation.getKeyFrame(elapsedTime, true), fireBallRect.getX(), fireBallRect.getY());
batch.end();
// Remove the fireball that collides with the bucket
iter.remove();
}
}
}
// Touch input controller
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
if(touchPos.y <= 250) {
bucketSpriteRect.setX(touchPos.x - 115);
bucketRect.setX(touchPos.x - 50); // <---- adjust position left/right on touch
}
}
if(TimeUtils.nanoTime() - lastFireballTime > fireballFrequency) {
fireBallRect = new Rectangle();
fireBallRect.x = MathUtils.random(50, w - 270);
fireBallRect.y = h;
fireBallRect.width = 75;
fireBallRect.height = 75;
fireballArray.add(fireBallRect);
lastFireballTime = TimeUtils.nanoTime();
}
/**************************************************************************************************/
break;
case PAUSE:
// Pause the game here
/******************************** Redraw the sprites to avoid flickering **************************/
batch.begin();
batch.draw(backgroundSprit, 0, 0, w, h);
batch.draw(bucketSprite, bucketSpriteRect.x, bucketSpriteRect.y,
bucketSprite.getWidth() - 160, bucketSprite.getHeight() - 140);
for(Rectangle fireball: fireballArray) {
batch.draw(fireballSprite, fireball.x, fireball.y,
fireballSprite.getWidth() - 20, fireballSprite.getHeight() - 20);
}
batch.draw(progressBarSkin.getSprite("progressBarBg"), 200, flipCoordinates(h, 170));
batch.draw(progressBarSkin.getSprite("progressBar"),
210, flipCoordinates(h, 162), barLength, 6); // <---- modify x, x, <x>, x to adjust the length of the life-bar
batch.draw(heartSprite, 95, flipCoordinates(h, 200), 90, 76);
//pauseButton.draw(batch, 1);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
batch.end();
/**************************************************************************************************/
break;
case RESUME:
break;
default:
break;
}
}
/************************************* Implement Dispose **************************************/
#Override
public void dispose() {
batch.dispose();
bucketRenderer.dispose();
fireballRenderer.dispose();
backgroundImage.dispose();
bucketImage.dispose();
fireballImage.dispose();
bucket_tap.dispose();
bucket_drag.dispose();
catchFireball.dispose();
bgMusic.dispose();
fireball_missed_sound.dispose();
progressBarSkin.dispose();
fballAtlas.dispose();
stage.dispose();
}
/************************************* Flip Coordinates Method ********************************/
public static float flipCoordinates(float height, float xORy) {
float newPosition = height - xORy;
return newPosition;
}
/************************************** Deduces Life Points ***********************************/
private void deductLifePoints() {
barLength -= deductedBarSegment;
}
/************************************** Set the Game's state **********************************/
public void setGameState(State s) {
this.state = s;
}
/******************************** Android's Lyfe-Cycle Overrides ******************************/
#Override
public void pause() {
super.pause();
this.state = State.PAUSE;
}
#Override
public void resume() {
super.resume();
this.state = State.PAUSE;
}
}
The first parameter of the Animation constructor is the frameDuration in seconds. You are passing it 1/15f, which is really fast. Try something greater, like 0.15f.
Related
TextButton does not receive clicks in LibGDX
Here is my code: public WelcomeScreen(final WallGame game) { this.game = game; // Setting up camera and viewport. camera = new OrthographicCamera(); viewport = new StretchViewport(GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera); viewport.apply(); stage = new Stage(viewport); camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); Gdx.input.setInputProcessor(stage); skin = new Skin(Gdx.files.internal("uiskin.json")); skin.getFont("default-font").getData().setScale(0.1f, 0.1f); backgroundTxtr = new Texture(Gdx.files.internal("background/background.png")); background = new Image(backgroundTxtr); background.setBounds(0, 0, GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT); final TextButton button = new TextButton("start", skin, "red-button"); button.setBounds(10, 10, 50, 50); button.setTouchable(Touchable.enabled); button.addListener(new ClickListener() { public void clicked(InputEvent event, float x, float y) { Gdx.app.log("AKS", "CLICKED"); } }); final Dialog dialog = new Dialog("Click Message", skin); dialog.setBounds(50, 50, 40, 40); stage.addActor(background); stage.addActor(button); stage.addActor(dialog); } I can't seem to make the button work, I have an image to be shown when the button is clicked (the "down" attribute of the TextButton) but it doesn't change. What am I doing wrong?
I figured it out. I hadn't created a Table object to add the button into. It worked after I did that.
How to make a sprite blink X seconds?
I'm using libGDX and I want to make my main character of my game to blinking x seconds on enemy touch (lose life) Can someone tell me how can I make sprite to blinding?
Here is my solution, I use it exactly for the same purpose you described (blinking character when he injured). public class Blinker { private float BLINK_TIME = 1f; private int BLINKING_FRAMES = 4; private boolean isBlinking; private int blinkFrameCounter; private float blinkTimer; public Blinker() { this.blinkTimer = 0; this.blinkFrameCounter = 0; this.isBlinking = false; } public boolean shouldBlink(float delta) { if (isBlinking) { blinkTimer += delta; blinkFrameCounter++; if (blinkTimer < BLINK_TIME) { if (blinkFrameCounter % BLINKING_FRAMES == 0) { return true; } } else { blinkTimer = 0; isBlinking = false; } } return false; } public boolean isBlinking() { return isBlinking; } public void setBlinking(boolean isBlinking) { this.isBlinking = isBlinking; } } Usage: first init the blinker object; Blinker blinker= new Blinker(); blinker.setBlinking(true); and then just add this to your draw() method (I suppose that you have separate method for drawing character, which you call in your screen draw method), before you draw the sprite you want to blink. if (blinker.shouldBlink(delta)) return;
I recommend looking at something like this. https://github.com/libgdx/libgdx/wiki/2D-Animation You use this technique and adjust the frame duration. walkAnimation = new Animation(0.025f, walkFrames); // #11 When you need to start making the animation blink faster. adjust the frame duration value. public void setFrameDuration(float frameDuration) I recommend doing it this way but if you HAVE to tint it w/e.... I guess this works. private Texture texture; private TextureRegion region; private Sprite sprite; ... texture = new Texture(Gdx.files.internal("image.png")); region = new TextureRegion(texture, 20, 20, 50, 50); sprite = new Sprite(texture, 20, 20, 50, 50); sprite.setPosition(100, 10); sprite.setColor(0, 0, 1, 1); ... batch.begin(); batch.setColor(1, 0, 0, 1); batch.draw(texture, 10, 10); batch.setColor(0, 1, 0, 1); batch.draw(region, 50, 10); sprite.draw(batch); batch.end(); key thing is Alpha is ignored if blending is disabled.
The previous answer from prgenhard will work, but I don't think that is sufficient enough, if you are using it without care. You can use TweenEngine to interpolate alpha value with different functions. It can be really cool. Those values you can set on sprite's or actor's alpha and easily achieve flashing effect. You can read more about it here: http://www.aurelienribon.com/blog/projects/universal-tween-engine/ It should be enough. And also it can be easily used within Libgdx engine. Hope this helps.
box2d dynamic body is not falling under gravity
I am trying to make a dynamic body which should move ahead a bit and then fall according to its gravity(as decided by the box2d engine). The problem is that right after moving upto a certain distance, instead of moving down, it is going up. This is the code i am using: public class AngryBirdsTrajectoryPrototype implements ApplicationListener { private SpriteBatch spriteBatch; private Texture backgroundTexture; private Sprite backgroundSprite; private static Sprite ball_in_hand; private Texture ball_in_hand_Texture; private static com.badlogic.gdx.physics.box2d.Body b2Body; private com.badlogic.gdx.physics.box2d.World box2Dworld; private boolean isBallShooted; #Override public void create() { spriteBatch = new SpriteBatch(); Texture.setEnforcePotImages(false); backgroundTexture = new Texture(Gdx.files.internal("angrybirds/background.png")); backgroundTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); backgroundSprite = new Sprite(backgroundTexture); backgroundSprite.setPosition(0, 0); ball_in_hand_Texture = new Texture(Gdx.files.internal("test/ball_in_hand.png")); ball_in_hand_Texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); ball_in_hand = new Sprite(ball_in_hand_Texture); this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F, 10.0F), true); } #Override public void render() { Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1f); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); if(!isBallShooted) { isBallShooted = true; shoot(this.box2Dworld, 27.0F, 0.F); } box2Dworld.step(1.0f/60.0f, 6, 2); spriteBatch.begin(); backgroundSprite.draw(spriteBatch); Array<Body> bodies = new Array<Body>(); box2Dworld.getBodies(bodies); for (Body body : bodies) { ball_in_hand.setPosition(30.0F * body.getPosition().x, 30.0F * body.getPosition().y); Gdx.app.error("", "angleDegree= " + body.getAngle()); } ball_in_hand.draw(spriteBatch); spriteBatch.end(); } #Override public void dispose() { spriteBatch.dispose(); backgroundTexture.dispose(); } public static void shoot(com.badlogic.gdx.physics.box2d.World var1, float var2, float var3) { BodyDef var4 = new BodyDef(); var4.position.set(ball_in_hand.getX() / 30.0F, ball_in_hand.getY() / 30.0F); var4.type = BodyType.DynamicBody; var4.bullet = true; var4.angularDamping = 0.5F; b2Body = var1.createBody(var4); CircleShape var6 = new CircleShape(); var6.setRadius(0.4F); FixtureDef var7 = new FixtureDef(); var7.density = 0.8F; var7.shape = var6; var7.restitution = 0.7F; var7.friction = 1.0F; b2Body.createFixture(var7); float var9 = var2 * 9.0F; Vector2 var10 = new Vector2(var9 * (float)Math.cos((double)var3), var9 * (float)Math.sin((double)var3)); b2Body.applyForce(var10, b2Body.getWorldCenter(), true); } } This is the output i am getting. This is the output i want to achieve: I have tried all possible options but none of them is working. Please help me. Thanks in advance.
Try this: You should set your gravity to a negative number because of the Box2d coordinate system, like this: enter code here`this.world = new World(new Vector2(0, -9.8f), true); You should create a camera for your physics world and convert all your sizes to meters. For example, you can set 100 pixels = 1 meter in the box2d world, so you will need to set your camera's width and height in meters, too. Here is a full working example. You can also get delta like this: Gdx.graphics.getDeltaTime() Please let me know if it helped.
I think the problem in your code is in the create method: this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F, 10.0F), true) You should use a negative value of gravity in y-direction like: this.box2Dworld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0.0F, -10.0F), true) Also, please use debug-renderer to spot the error.
As3 hittesting doesn't really work (nested)
I got a tank. I got a hero (controllable character). The tank has a nested movieclip which has a very thin surface area. Yet, the thing detects a collision when it's not even touching the hero. public class tank_sight extends MovieClip { private var _root:MovieClip; public function tank_sight() { addEventListener(Event.ADDED, beginClass); } private function beginClass(event:Event):void { _root = MovieClip(root); addEventListener(Event.ENTER_FRAME, loop); } private function loop(event:Event):void { if(this.hitTestObject(_root.hero.hitbox)) { this.gotoAndStop(2); trace("HIT"); fire(); } else { this.gotoAndStop(1); } } private function fire():void { var shell:Shell = new Shell(x, y, rotation - 180); _root.addChild(shell); } } What's wrong? I don't get it. EDIT: Sight is rotating, so that's probably why. I tried using this code on the player class: point = _root.tanks.barrel.sight.localToGlobal(new Point()); if(this.hitTestPoint(point.x, point.y, false)) { trace("HIT"); } But it doesn't work.. It never traces "HIT", unless I stand in some weird location at certain times.
hitTestObject works with nested objects also (display objects that have different parents), you should check logic of your game, and do some tests. Simple example: var squareHolder:Sprite = new Sprite(); var squareInner:Shape = new Shape(); var hitHolder:Sprite = new Sprite(); var hitCircle:Shape = new Shape(); hitCircle.graphics.beginFill(0x990000); hitCircle.graphics.drawCircle(0, 0, 20); squareInner.graphics.beginFill(0x009900); squareInner.graphics.drawRect(0, 0, 40, 40); addChild(squareHolder); squareHolder.addChild(squareInner); squareHolder.x = 200; squareHolder.y = 100; squareInner.x = 50; squareInner.y = 50; stage.addChild(hitHolder); hitHolder.addChild(hitCircle); hitCircle.transform.matrix = new Matrix(1, 0.5, 0.5, 1, 30, 30); stage.addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void { hitHolder.x = e.stageX; hitHolder.y = e.stageY; if (hitCircle.hitTestObject(squareInner)) { trace("Ding!"); } }); Whatever you do with hitCircle (visible=false, trasparent fill, transformations) it will still work.
five3d local3dtoglobal
I am trying to use the Five3d package to draw a sprite in 3d and animate it then create a shape on the root of the app and overlay it over the sprite in 3d as it moves. I have tried everything to get it to work but can't seem to get it to line up properly. It looks like the perspective is out. I had it working after inspecting the relationship between the 3d x,y and global x,y which lead me to write the method 'local3dToGlobalXY' shown below but this only worked when the stage was 1024 / 768, strange. I was told that the Sprite2d in the Five3d package does this but can't get that to work either. It has the same results as my 'local3dToGlobalXY' method. I have put an example up here and the project for the example is here. The red star is a sprite that is in 3d and the two crosshairs are draw into different scopes. One is drawn into a Sprite2d which is what I was recomended to do and the other is drawn into the root. Hopefully someone has encountered this or can point out what I have done wrong. Thanks, J /** * ... * #author James Jordan */ public class Main extends Sprite { private var s3D:Sprite3D; private var _scene:Scene3D; private var _s:Sprite3D; private var star3D:Shape3D; private var t:Timer; private var crossHairs2D:Sprite; private var otherPlane:Sprite2D; private var _plane:Sprite3D; private var crossHairsRoot:Sprite; private var animationPlane:Sprite3D; private var crossHairsPlane:Sprite2D; private var starsPlane:Sprite3D; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; animationPlane = new Sprite3D(); starsPlane = new Sprite3D(); animationPlane.addChild(starsPlane); crossHairsPlane = new Sprite2D(); animationPlane.addChild(crossHairsPlane); _scene = new Scene3D(); _scene.x = stage.stageWidth / 2; _scene.y = stage.stageHeight / 2; _scene.addChild(animationPlane); addChild(_scene); star3D = drawStar(starsPlane); crossHairs2D = drawGlobalShape(crossHairsPlane, 0xff000, 10); crossHairsRoot = drawGlobalShape(root, 0x0000ff, 5); t = new Timer(1000, 0); t.addEventListener(TimerEvent.TIMER, onTimer); t.start(); } private function onTimer(e:TimerEvent):void { TweenLite.to(star3D, 1, { x:(Math.random() * stage.stageWidth) - (stage.stageWidth/2), y:(Math.random() * stage.stageHeight) - (stage.stageHeight / 2), z:(Math.random() * 500), onUpdate:onTweenUpdate } ); } private function onTweenUpdate():void { var p:Point = local3dToGlobalXY(starsPlane, new Point3D(star3D.x, star3D.y, star3D.z)); crossHairs2D.x = p.x - stage.stageWidth / 2; crossHairs2D.y = p.y - stage.stageHeight / 2; crossHairsRoot.x = p.x; crossHairsRoot.y = p.y; } private function local3dToGlobalXY(_s:Sprite3D, p3d:Point3D):Point { var m:Matrix3D = _s.concatenatedMatrix; var newP3d:Point3D = m.transformPoint(p3d); var globalPoint:Point = _s.local3DToGlobal(new Vector3D(newP3d.x, newP3d.y, newP3d.z)); return globalPoint; } private function drawStar(parent:Sprite3D):Shape3D { var starShape:Shape3D = new Shape3D(); DrawingUtils.star(starShape.graphics3D, 5, 30, 20, (45 / 180) * Math.PI, 0xff0000); parent.addChild(starShape); return starShape; } private function drawGlobalShape(p:*, c:uint, lineWeight:Number):Sprite { var d:Sprite = new Sprite(); d.graphics.lineStyle(lineWeight, c); d.graphics.moveTo( -20, 0); d.graphics.lineTo(20, 0); d.graphics.moveTo( 0, -20); d.graphics.lineTo(0, 20); d.graphics.endFill(); p.addChild(d); return d; } }
It's your perspective and Z coordinate. Remove the random Z tween in the TweenLite.to statement and you'll see that they line up perfectly. What exactly are you trying to do here? Other than offset the star, the Z param doesn't seem to add anything (scaling based on distance from observer)?