GetPixel for a bitmap under another? - actionscript-3

Any help on this would be much appreciated.
I am creating an interactive map in flash / actionscript 3.0 and would like to allow a user to click on a location in the map in order to find the elevation at that point. In the stage, I have a base map of the area sitting on top of a black and white image where the value of each pixel represents height in feet.
So far, using getPixel to retrieve the elevation works great, but when the base map is sitting on top of the black and white elevation surface, getPixel retrieves values for the base map, not the underlying image. Is there a way to display the base map to the user while still using getPixel to retrieve values from the underlying image?
Many thanks,
Matt

Simply use getPixel() on the black / white image, not the container.
I assume you have a container Sprite and 2 children, the black / white image and on top the base map. Attach the click listener to the container Sprite and retrieve the first child with getChildAt(0). Get the BitmapData of that child and call getPixel(x, y) on it.

If the underlying image is not being shown to the user, it need not even be in the display list; it can exist in memory only as a BitmapData.
Add your MouseEvent.CLICK listener function to the base map users can click on, and in that function use the event x and y to do a BitmapData.getPixel(x,y) on your elevation map (greyscale image) BitmapData object.

Related

libGDX repeated background texture drawing difficulties

Like everyone else, I'm having trouble following how libgdx's coordinate transformations. I'm creating a scrabble-like game, and dragging a finger on the screen pans the camera. The tiles are Actors on a Stage, and I'm doing the camera transformations on the stage's camera. That all works. Now I'm trying to give it a fancy background for the tiles to sit on. I can't quite figure out the draw method to make it work.
//Assets class
static final Texture feltBackground = new Texture(
Gdx.files.internal("felt_background.png"));
feltBackground.setWrap(Texture.TextureWrap.Repeat,
Texture.TextureWrap.Repeat);
//board rendering snippet
private void drawBackground() {
batch.setProjectionMatrix(board.getCamera().combined);
batch.begin();
screenUpperLeft.set(0, 0, 0);
screenLowerRight.set(Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(), 0);
board.getCamera().unproject(screenUpperLeft);
board.getCamera().unproject(screenLowerRight);
batch.draw(Assets.feltBackground,
0, 0,
(int)screenUpperLeft.x, (int)screenUpperLeft.y,
(int)screenLowerRight.x, (int)screenLowerRight.y);
batch.end();
}
What I'm attempting in drawBackground is:
set boundary points to screen bounds
unproject those points into world space to find the region of the texture I should draw
draw that region to the screen origin
However, with the code like this I'm having various weird problems like the lower boundary of the background region starting offscreen, the upper boundary moving when the camera is zoomed, the texture panning faster than my finger's movement (although the boundaries of the background region pan correctly), the y axis pans mirrored, etc. Changing the code changes these symptoms, but I haven't found a pattern to try to get closer to being correct. Any words of wisdom for drawing like this?
Edit:
Here are some screenshots to add clarity.
When I open the game, there is no background.
I can drag up, which moves up the upper boundary of the background (if there is a lower boundary, I can't ever find it)
I can drag the left boundary right, but like the bottom I can't ever find a right boundary (if it exists)
Whenever you see weird synchronization issues between your camera and the stuff you're trying to draw, it's generally a symptom of confusing world coordinates with viewpoint coordinates (or vise versa) somewhere in your code.
You're using a SpriteBatch to do the rendering, right? When I look up the API for the version of SpriteBatch#draw() which you are calling, it says that those first two floats are the position that you're rendering the sprite in the world, and the four integers have to do with the source and height of the source image.
When you pass (0,0) as the position, it is drawing the image at 0,0 in your game world, which is not (0,0) on your viewport.
I would recommend a simpler approach- instead of trying to project or unproject the coordinates, set the draw location (those first two floats) to stage.getCamera().position.x and stage.getCamera().position.y.

Sprite positioning using mouse coordinates

I am trying to move a sprite to the mouse position on click.
However, the coordinates I am getting from Gdx.input.getX()and Gdx.input.getY() is relative to the top left corner, and the setPosition() method of Sprite is relative to the bottom left corner.
Why is this so, and how do I position my sprite where the mouse was clicked?
Screen coordinates come from Android and use a Y-down frame of reference. Cameras in libgdx by default use a Y-up frame of reference (because OpenGL by convention also uses a Y-up frame of reference).
If you prefer the Y-down frame of reference, you can use camera.setToOrtho(true); method to flip it upside down. You might prefer this if coming from a Flash background.
But in general, the safe way to translate screen coordinates from a touch into the camera's coordinate system is to do the following. This will work regardless of what platform you're on and whatever coordinate system you chose for the camera. For example, for some types of games, you wouldn't even be using a camera that matches the screen resolution, but you'd still want screen coordinates converted to camera coordinates. Also, if you have a camera that moves around the world, this will automatically change the touch point to world coordinates.
tempVector3.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(tempVector3);
//now tempVector3 contains the touch point in camera coordinates.
It uses Vector3 because this also works for 3D cameras.

Drawing the exact region of the Bitmap in as3

I am working on a Action Script 3.0 application , in which i ill be allowed to load the image and make them draggable. Consider i am loading the deer image and making it as draggable.
Problem with this is , if i click on the translucent area ( white space around the bitmap ), i dont want the bitmap to draggable.is there any way to draw the deer boundary region exactly without the white space around it.
You can use BitmapData methods to get each pixel color, and then, you can either :
On creation, for each pixel if it's not fully transparent (!= 0) you can draw a point of a Shape, which will be transparent, and make it dragable in place of your bitmap (as noticed in comment, it will be quite CPU consuming, so use the second method)
On click, get the click coordinate relative to the bitmap, check if the pixel is transparent and make it drag only if it's not.
In either way, that will be quite CPU consuming. You may also consider convert your bitmap to a vector image (Sprite). This will allow flash to detect real images boundaries.

Dragging an object along a drawn path

Same as the question:
I need to drag a component along a programmatically drawn path composed by different kinds of graphic, like lines, curves, etc.
I've already googled it, but with no success.
Any ideas? thanks!
The following is say for a linearly curved path drawn by you. You can use similar method for any direction.
Add an Event listener for click.
(That starts drag)
Track the user's mouse along x
direction for example.
Keep plotting the component's x & y as
Mouse x changes with respect to the
drawn path's x.
Stop relocating as user leaves the
mouse
Start with this if possible & be back with code if you get doubts.
If your drawing part is complete then You can use two dimensional ByteArray. The size of the ByteArray will be the size of your stage, this two-dimensional array will be set to zero, means all your stage locations are set to zero. When any curve or line is drawn , set those locations to one. Now you know at-least where your object can move, the valid locations are those which are set to one.
Now the second the part is how to move an object on the valid path or location using mouse or keyboard.
You will be using Event.EnterFrame for smooth and fast movement of the object,
1--using keyboard.
use up key to move object to upper location if that position or location is set to one else the object will not move Up, same for others.
2-- using mouse move event, detect MouseY position for moving UP or DOWN w.r.t current position of MouseY , and move it respectively if location is set to one.
Hope, this will guide you in right direction...
Will this work? http://www.pixeldigest.com/followthepath.html

Is it possible to move the stage in actionscript 3.0?

Hi to keep it short and simple let's say I have a stage with 400x400 size in pixels, but I've drawn a map of 1000x1000 size in pixels. I want my player to be able to "walk" about the stage, but it appears stage.x and stage.y are read-only? Is there any method or way to have the stage "scroll" about, without having to move each object on the map?
Don't move the stage, move the 1000x1000 object,then it'll look like the whole thing is moving.
You should see the stage like a window. You can see everything behind it depending on the size of the window. You cannot change the size of the stage, or move it.
Just like a window you can measure the size of the stage. You can use this to navigate for example movieclips across the stage with actionscript.
Why don't you put the map and the other objects in a seperate layer, and move the map around. Other objects (for example a big red dot to tell the user its' location on the map) are on a fixed position on the map. Just move the map following a sort of path according the red dot.
Not entirely sure what you want to do, but it isn't possible to move the stage.
You can put all the movieclips (the player and the map, if you want) in one movieclip, put only that movieclip on the stage and move that.
But if you only want the map to scroll, just move the map around.
The other answers are correct, but there's an alternative to moving the map:
ScrollRect
Attach a rectangle to a your map's scrollRect property. Moving that rectangle will have the same apparent effect as moving the stage around.
There are minor pros and cons to using scrollRect vs. moving the world, but try them both and see which works better for you.