Read that the default size should be 12px. Is this something about DPI? How to solve?
EDIT
Thank you folks, but this doesn't seams to be something related to the font size. It looks like its something about handling DPI because it happens with simple Sprites too and weirdly the colors on Android seams to use an inversed RGB sequence 🤔
The Main.hx:
package;
import openfl.display.DisplayObjectContainer;
import openfl.display.Sprite;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.events.Event;
import openfl.system.Capabilities;
import openfl.text.TextField;
import ru.stablex.ui.UIBuilder;
class Main extends Sprite
{
private var size: Float = 100;
private var r: Sprite;
private var g: Sprite;
private var b: Sprite;
public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
}
private function addedToStage(event: Event): Void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
r = createSquare(this, 0x00FF0000, size);
g = createSquare(this, 0x0000FF00, size);
b = createSquare(this, 0x000000FF, size);
stage.addEventListener(Event.RESIZE, resize, false, 0, true);
resize(null);
}
private function resize(event: Event): Void
{
r.x = 0;
r.y = 0;
g.x = (stage.stageWidth / 2) - (size / 2);
g.y = (stage.stageHeight / 2) - (size / 2);
b.x = stage.stageWidth - size;
b.y = stage.stageHeight - size;
}
private function createSquare(parent: DisplayObjectContainer, color: Int, size: Float): Sprite
{
var box: Sprite = new Sprite();
box.graphics.beginFill(color);
box.graphics.drawRect(0, 0, size, size);
box.graphics.endFill();
parent.addChild(box);
return box;
}
}
You can use sp size unit, which always seems the same size on different dpi screens.
Please try this.
text.autoSize = "center";
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextFieldAutoSize.html
You can size Sprite size to 100%.
Auto size text field with Actionscript 3 -- This link can help you.
Related
I want to create multiple independent Texture objects with key words on it falling from top to bottom and a key board displaying at bottom to type the letter on the texture object to capture it and generate new objects repeatedly for given time interval I have gone through the wiki's code for help but when I'm trying to display the words on the texture objects they change the letter on every Texture object on the Batch
package com.example.jtech.bubbletypinggame;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import java.util.Iterator;
public class BubbleTypingGame extends ApplicationAdapter {
private Texture [] dropImage = new Texture[5];
private Texture bucketImage;
private Texture background;
private Sound dropSound;
private Music rainMusic;
private SpriteBatch batch;
private OrthographicCamera camera;
private Rectangle bucket;
private Array<CustomRectangle> raindrops;
private Array<String> rainKeyWords;
private Sprite mySprite;
CustomRectangle raindrop;
private long lastDropTime;
String[] chars = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int i = 0;
String captionString;
private BitmapFont font;
int j = 0;
int x = 10, y = 740;
#Override
public void create() {
font = new BitmapFont();
font.setColor(Color.RED);
//load backgroud image for the game
background = new Texture("background_nebula.jpg");
// load the images for the droplet and the bucket, 64x64 pixels each
dropImage [0] = new Texture(Gdx.files.internal("balloon_a.png"));
dropImage [1] = new Texture(Gdx.files.internal("balloon_b.png"));
dropImage [2] = new Texture(Gdx.files.internal("balloon_c.png"));
dropImage [3] = new Texture(Gdx.files.internal("balloon_d.png"));
dropImage [4] = new Texture(Gdx.files.internal("balloon_e.png"));
// 25JANwORKING//
//25JANWORKING//
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
// load the drop sound effect and the rain background "music"
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
// start the playback of the background music immediately
rainMusic.setLooping(true);
rainMusic.play();
// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
// create a Rectangle to logically represent the bucket
bucket = new Rectangle();
bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
bucket.width = 64;
bucket.height = 64;
// create the raindrops array and spawn the first raindrop
raindrops = new Array<CustomRectangle>();
rainKeyWords = new Array<String>();
spawnRaindrop();
}
private void spawnRaindrop() {
raindrop = new CustomRectangle();
raindrop.x = MathUtils.random(0, 800-100);
raindrop.y = 480;
raindrop.width = 100;
raindrop.height = 50;
raindrop.keyWord = "k";
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
if(i==chars.length){
i=0;
}
captionString = chars[i];
rainKeyWords.add(captionString);
i++;
}
private void displayKeyWord(){
if(i==chars.length){
i=0;
}
captionString = chars[i];
rainKeyWords.add(captionString);
lastDropTime = TimeUtils.nanoTime();
i++;
}
#Override
public void render() {
// clear the screen with a dark blue color. The
// arguments to glClearColor are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// tell the camera to update its matrices.
camera.update();
// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
batch.setProjectionMatrix(camera.combined);
// begin a new batch and draw the bucket and
// all drops
batch.begin();
if(j==5){
j=0;
}
batch.draw(background,0,0);
batch.draw(bucketImage, bucket.x, bucket.y);
batch.draw(dropImage[j], raindrop.x, 400);
/*for(Rectangle raindrop: raindrops) {
batch.draw(dropImage[i], raindrop.x, raindrop.y);
//font.draw(batch, captionString, raindrop.x+45, raindrop.y+30);
}*/
batch.end();
/*// process user input
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
bucket.x = touchPos.x - 64 / 2;
}
if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
// make sure the bucket stays within the screen bounds
if(bucket.x < 0) bucket.x = 0;
if(bucket.x > 800 - 64) bucket.x = 800 - 64;
// check if we need to create a new raindrop*/
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
// move the raindrops, remove any that are beneath the bottom edge of
// the screen or that hit the bucket. In the later case we play back
// a sound effect as well.
Iterator<CustomRectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 100 * Gdx.graphics.getDeltaTime();
if(raindrop.y + 64 < 0) iter.remove();
if(raindrop.overlaps(bucket)) {
dropSound.play();
iter.remove();
}
}
}
#Override
public void dispose() {
// dispose of all the native resources
dropImage[j].dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
batch.dispose();
}
}
Now, the code where it looks like you draw the letters are commented out, but I assume this is what you intend to use.
for(Rectangle raindrop: raindrops) {
batch.draw(dropImage[i], raindrop.x, raindrop.y);
font.draw(batch, captionString, raindrop.x+45, raindrop.y+30);
}
What this means: for each object in the raindrops array, you draw the captionString. So if you have 20 objects in raindrops and captionString = "a", then you will draw the letter "a" 20 times.
You are using a single string to represent 20 strings. A String can only hold a single value. So, the last value you give captionString, is the only value you can display using it. You don't change the value in the loop.
Every raindrop object needs to have its own string value. And this value is the one you will need to draw, not captionString.
[edit]
Looks like you already have this in place. Just use the right string.
Try:
for(Rectangle raindrop: raindrops) {
batch.draw(dropImage[i], raindrop.x, raindrop.y);
font.draw(batch, raindrop.keyWord, raindrop.x+45, raindrop.y+30);
}
OK for sake of argument and simplicity this code here has a rectangle sprite/texture that shoots(cuz it's a gun) upwards. And an enemy rectangle/sprite/texture the spawns downwards. Then the player detects if it hits a enemy. When the player hits an enemy I get an out of bounds exception -1
package com.TheGame.Pack;
import java.util.Iterator;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
public class GameScreen implements Screen {
final MasterClass game;
Texture FleetTexture;
Texture PlayerTexture;
Texture ShootingTexture;
OrthographicCamera camera;
Rectangle Player;
Array<Rectangle> Emma;
Array<Rectangle> Shooting;
long EmmaSpawnTime;
long ShootingTime;
public static int EmmaKilled = 0;
public GameScreen(final MasterClass gam) {
this.game = gam;
// load the images for the droplet and the Player, 64x64 pixels each
FleetTexture = new Texture(Gdx.files.internal("cirA.png")); //Enemies
PlayerTexture = new Texture(Gdx.files.internal("BoxA.png"));
ShootingTexture = new Texture(Gdx.files.internal("gun.png"));
// load the drop sound effect and the rain background "music"
// dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
// rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
// rainMusic.setLooping(true);
// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
// create a Rectangle to logically represent the Player
Player = new Rectangle();
Player.x = 800 / 2 - 64 / 2; // center the Player horizontally
Player.y = 20; // bottom left corner of the Player is 20 pixels above
// the bottom screen edge
Player.width = 40;
Player.height = 30;
// create the Emma array and spawn the first EmmaInArray
Emma = new Array<Rectangle>();
Shooting = new Array<Rectangle>();
spawnEmma();
}
private void spawnEmma() {
Rectangle EmmaInArray = new Rectangle();
EmmaInArray.x = MathUtils.random(0, 800 - 64);
EmmaInArray.y = 480;
EmmaInArray.width = 40;
EmmaInArray.height = 30;
Emma.add(EmmaInArray);
EmmaSpawnTime = TimeUtils.nanoTime();
}
private void spawnShooting(){
Rectangle ShootingInArray = new Rectangle();
ShootingInArray.x = Player.x;
ShootingInArray.y = Player.y;
ShootingInArray.width = 40;
ShootingInArray.height = 30;
Shooting.add(ShootingInArray);
ShootingTime = TimeUtils.nanoTime();
}
#Override
public void render(float delta) {
// clear the screen with a dark blue color. The
// arguments to glClearColor are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// tell the camera to update its matrices.
camera.update();
// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
game.batch.setProjectionMatrix(camera.combined);
// begin a new batch and draw the Player and
// all drops
game.batch.begin();
game.font.draw(game.batch, "Drops Collected: " + EmmaKilled, 0, 480);
game.batch.draw(PlayerTexture, Player.x, Player.y, Gdx.graphics.getWidth() / 20,
Gdx.graphics.getHeight()/ 20);
for (Rectangle EmmaInArray : Emma) {
game.batch.draw(FleetTexture, EmmaInArray.x, EmmaInArray.y);
}
for(Rectangle ShootingInArray : Shooting){
game.batch.draw(ShootingTexture, ShootingInArray.x, ShootingInArray.y);
ShootingInArray.y +=10;
}
game.batch.end();
// process user input
if (Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
Player.x = touchPos.x - 64 / 2;
}
if (Gdx.input.isKeyPressed(Keys.LEFT))
Player.x -= 400 * Gdx.graphics.getDeltaTime();
if (Gdx.input.isKeyPressed(Keys.RIGHT))
Player.x += 400 * Gdx.graphics.getDeltaTime();
// make sure the Player stays within the screen bounds
if (Player.x < 0)
Player.x = 0;
if (Player.x > 800 - 64)
Player.x = 800 - 64;
// check if we need to create a new EmmaInArray
if (TimeUtils.nanoTime() - EmmaSpawnTime > 100000000){
spawnEmma();
}
if(TimeUtils.nanoTime() - ShootingTime > 100000000){
spawnShooting();
}
// move the Emma, remove any that are beneath the bottom edge of
// the screen or that hit the Player. In the later case we play back
// a sound effect as well.
Iterator<Rectangle> EmmaIterator = Emma.iterator();
while (EmmaIterator.hasNext()) {
Rectangle EmmaInArray = EmmaIterator.next();
EmmaInArray.y -= 200 * Gdx.graphics.getDeltaTime();
if (EmmaInArray.y + 64 < 0){
EmmaIterator.remove();
}
Iterator<Rectangle> ShootingIterator = Shooting.iterator();
while(ShootingIterator.hasNext()){
Rectangle ShootingInArray = ShootingIterator.next();
// ShootingInArray.y += 200 * Gdx.graphics.getDeltaTime();
if(ShootingInArray.y > 480){
ShootingIterator.remove();
}
if(EmmaInArray.overlaps(ShootingInArray)){
ShootingIterator.remove();
EmmaIterator.remove();
}
if (Player.overlaps(EmmaInArray)) {
EmmaKilled++;
game.setScreen(game.HS);
// dropSound.play();
if I comment out EmmaIterator.remove(); it runs fine with it uncommented it crashes upon hit.
Why does this crash is this not the proper way to do this? Do I need to somehow detect hit's at the same time? How can the array be at negative 1 when clearly there are still enemies on the screen?
EmmaIterator.remove();
}
}
Though this is not the way I will have things setup this code still should run with no issues. I encounter the same problem when instead of detecting the player enemies collisions I have 2 guns checking for collisions. This seems like a big problem to me which is why I'd say I'm just doing it wrong but documentation is light so I come here.
}
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
// start the playback of the background music
// when the screen is shown
//rainMusic.play();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
FleetTexture.dispose();
PlayerTexture.dispose();
ShootingTexture.dispose();
// dropSound.dispose();
// rainMusic.dispose();
}
}
It's unlikely, unless multithreading is being used, that anything will happen at the same time exactly. You have a number of probable typos in your code, but one will break it:
Rectangle var1_holder = iter.next();
That same reference to iter is in both the first block, which should use it, and the second block, which should use iter1. You should consider using matching variable names, like
Iterator<Rectangle> iter3 = var3.iterator();
if you must have numbers as the only distinguishing feature.
notostraca is right. But to make it more clear i will show u an example with for loops which i use for collisions. it can't make any harm and i hope it will make it more clear for u
int v2 = var2.size();
for (int i = 0; i < v2; i++) {
if (object.getBounds().overlaps(var2.get(i).getBounds())) {
var2.remove(i);
v2--;
//in this line u might use break; if u know that just one object
//from var2 array can hit at a time
}
}
i write this code and i don't know how to fix all error of this code but this code work correctly in pc mode but give error in android mode this is my code :
var background : Texture2D;
var splash : Texture2D;
var font : Font;
private var showADPresents = 3.0;
var size = null;
static var virtualScreen : Vector2 = Vector2(800, 600);
function Update()
{
showADPresents -= Time.deltaTime;
if (Application.CanStreamedLevelBeLoaded(1) && Input.anyKeyDown)
Application.LoadLevel(1);
}
function OnGUI()
{
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(Screen.width / virtualScreen.x, Screen.height / virtualScreen.y, 1));
GUI.DrawTexture(Rect(0, 0, virtualScreen.x, virtualScreen.y), splash);
if (showADPresents > 0)
{
var alpha = Mathf.Clamp01(showADPresents);
var color = Color.white;
color.a = alpha;
GUI.color = color;
GUI.DrawTexture(Rect(0, 0, virtualScreen.x, virtualScreen.y), background);
var presentsStyle = new GUIStyle(GUI.skin.label);
presentsStyle.font = font;
presentsStyle.fontSize = 48;
size = 300;
color = Color.black;
color.a = alpha;
GUI.color = color;
GUI.Label(Rect(( virtualScreen.x - size) * 0.5, (virtualScreen.y - size) * 0.5, size, size), "Autodesk Presents:", presentsStyle);
}
else
{
var notificationStyle = new GUIStyle(GUI.skin.label);
notificationStyle.font = font;
notificationStyle.fontSize = 16;
GUI.color = Color.black;
var content = null;
if (Application.CanStreamedLevelBeLoaded(1))
content = new GUIContent("Click Screen to Continue");
else
content = new GUIContent("Loading...");
size = notificationStyle.CalcSize(content);
GUI.Label(Rect((virtualScreen.x - size.x) * 0.5, virtualScreen.y - size.y - 80, size.x, size.y),
content, notificationStyle);
}
}
and unity give me this error i cant solve this error plz help to me help :'(
http://upload.ghashang.com/images/gm214joveg1z354rkli.jpg
plz Belief i cant solve this error plz help me
plz solve all error
This is because size is not a static type. It is dynamic, and because there is no operator for a object subtracted from a float, this error is thrown. You have two options. One, you can cast as a float before you subtract.
GUI.Label(Rect(( virtualScreen.x - (float)size) * 0.5, (virtualScreen.y - (float)size) * 0.5, size, size), "Autodesk Presents:", presentsStyle);
The other, is to declare size as a float or int in the declaration.
float size = 0.0f;
//or
int size = 0;
However, later you try to reference size as though it was a Rect, which causes other errors. I would a)declare size as a Rect b)access the respective members of size. Your code will look like:
Rect size;
...
//remove the size = 300; line and replace it with a constructor
GUI.Label(Rect(( virtualScreen.x - size.x) * 0.5, (virtualScreen.y - size.y) * 0.5, size, size), "Autodesk Presents:", presentsStyle);
This will solve your issue, and I recommend you read more about static classes
UPDATE Looks like this is a problem because of the static notification bar on tablet because of the lack of hardware buttons. I just didn't think about that. Anyway, in the case of the TF101 it returns a resolution of 1280x752 so about 1.702 (80 : 47) ratio. If I use a suitable unit size, like 33.5 or 11.75 vertically I get the proper scaling and this seems to fix the problem of skewed pixels.
END UPDATE
I've been setting up a game using 16x16 units for my tiled maps. I am using the resolution 1280x800 on both my desktop and android projects, I'm testing this to get a sense of how it will look on my TF101 asus tablet. I currently use a camera with units of 20x12.5 (wxh) and notice no pixel scaling on my desktop project, but when I run the game on my android I get weird scaling, and a green horizontal line. I can also move about quarter cell further along the x-axis on the tablet, shown in the screen shots. The pixels on the android project don't seem uniform at all.
I set the verticalTiles amount to 12.5f, then calculate the horizontalTiles amount as
verticalTiles = 12.5f;
...
horizontalTiles = (float) width / (float) height * verticalTiles;
camera = new OrthographicCamera(horizontalTiles, verticalTiles);
I'm aiming for devices with different aspect ratios to simply see more or less of the map, but can't seem to get working correctly. Any help would be appreciated.
Android Capture - http://imageshack.us/f/7/dsvg.png/ - notice the highlights on the roof edges, they are not uniform at all.
Desktop Capture - http://imageshack.us/f/853/5itv.png/
Current MainGame class
package com.bitknight.bqex;
/* Bunch of imports */
public class MainGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch spriteBatch;
private TiledMap map;
private OrthogonalTiledMapRenderer mapRenderer;
private Texture texture;
private Texture clothArmor;
private Sprite sprite;
private BitmapFont font;
private float horizontalTiles = 0;
private float verticalTiles = 12.5f;
private int hoverTileX = 0;
private int hoverTileY = 0;
private TiledMapTileLayer layer;
private Cell cell;
private TiledMapTile canMoveToTile;
private TiledMapTile cannotMoveToTile;
private AnimatedTiledMapTile animatedStopTile;
private AnimatedTiledMapTile animatedGoTile;
private Texture spriteSheet;
private TextureRegion region;
private Player player;
float h, w;
float ppuX, ppuY;
#Override
public void create() {
// Setup the animated tiles
Array<StaticTiledMapTile> tileArray;
// Start position on the sheet
int startX = 192;
int startY = 1568;
spriteSheet = new Texture(Gdx.files.internal("data/maps/tilesheet.png"));
spriteSheet.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
// We are trying to load two strips of 4 frames, 8 total
for( int i = 0; i < 2; ++i ) {
tileArray = new Array<StaticTiledMapTile>(4);
for( int j = 0; j < 4; ++j ) {
region = new TextureRegion(spriteSheet, startX, startY, 16, 16);
tileArray.add(new StaticTiledMapTile(region));
startX += 16;
}
if( i == 0 ) {
animatedStopTile = new AnimatedTiledMapTile(1/10f, tileArray);
} else {
animatedGoTile = new AnimatedTiledMapTile(1/10f, tileArray);
}
}
// Load the map
map = new TmxMapLoader().load("data/maps/base.tmx");
// Setup the two tiles that show movable and not movable sprites
canMoveToTile = map.getTileSets().getTileSet(0).getTile(1959);
canMoveToTile.setBlendMode(BlendMode.ALPHA);
cannotMoveToTile = map.getTileSets().getTileSet(0).getTile(1958);
cannotMoveToTile.setBlendMode(BlendMode.ALPHA);
// Manually create the layer used to show the cursor sprites
layer = new TiledMapTileLayer(100, 100, 16, 16);
layer.setName("display");
cell = new Cell();
cell.setTile(canMoveToTile);
layer.setOpacity(1f);
mapRenderer = new OrthogonalTiledMapRenderer(map, 1/16f);
spriteBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/consolas.fnt"), false);
font.setScale(0.6f);
texture = new Texture(Gdx.files.internal("data/maps/tilesheet.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
clothArmor = new Texture(Gdx.files.internal("data/img/native/clotharmor.png"));
region = new TextureRegion(clothArmor, 32, 256, 32, 32);
sprite = new Sprite(region);
sprite.setOrigin(0.5f, 0.5f);
sprite.setPosition(0f - 0.5f, 0f);
sprite.setSize(2, 2);
// Setup player and associated animations
Array<TextureRegion> regions = new Array<TextureRegion>();
player = new Player();
}
#Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
clothArmor.dispose();
spriteSheet.dispose();
}
#Override
public void render() {
player.update(Gdx.graphics.getDeltaTime());
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if( Gdx.input.isKeyPressed(Input.Keys.ESCAPE) ) {
Gdx.app.exit();
}
// Clear the last cell
layer.setCell(hoverTileX, hoverTileY, null);
// Convert screen coordinates to world coordinates
Vector3 worldCoordinates = new Vector3(Gdx.input.getX(0), Gdx.input.getY(0), 0);
camera.unproject(worldCoordinates);
hoverTileX = (int)(worldCoordinates.x);
hoverTileY = (int)(worldCoordinates.y);
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get("collision");
if( Gdx.input.isTouched(0) ) {
//sprite.setPosition(hoverTileX - 0.5f, hoverTileY);
player.pos.x = hoverTileX - 0.5f;
player.pos.y = hoverTileY - 0.25f;
cell.setTile(animatedGoTile);
} else {
if (layer.getCell(hoverTileX, hoverTileY) != null) {
cell.setTile(cannotMoveToTile);
} else {
cell.setTile(canMoveToTile);
}
}
layer.setCell(hoverTileX, hoverTileY, cell);
mapRenderer.setView(camera);
mapRenderer.render();
mapRenderer.getSpriteBatch().begin();
mapRenderer.renderTileLayer(layer);
mapRenderer.getSpriteBatch().end();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
player.render(spriteBatch);
spriteBatch.end();
}
#Override
public void resize(int width, int height) {
horizontalTiles = (float) width / (float) height * verticalTiles;
camera = new OrthographicCamera(horizontalTiles, verticalTiles);
w = width;
h = height;
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Looks like this is a problem because of the static notification bar on tablet because of the lack of hardware buttons. I just didn't think about that. Anyway, in the case of the TF101 it returns a resolution of 1280x752 so about 1.702 (80 : 47) ratio. If I use a suitable unit size, like 33.5 or 11.75 vertically I get the proper scaling and this seems to fix the problem of skewed pixels.
Also, while this is good for the TF101 tablet in my case it's not really a great solution. Here is a Gemserk series that talks about a nice solution.
http://blog.gemserk.com/2013/01/22/our-solution-to-handle-multiple-screen-sizes-in-android-part-one/
Using flex / air 2.6 working on an Android device, I am using a canvas with a TransformGestureEvent for zoom in on a map. The listening of the efect is working ok, but I can´t seem to find the information on wether the user intended zoom in or zoom out (fingers getting closer or getting apart).
I expected the intention of the gesture to be identifiable with the offsetX and offsetY properties of the event, as you will do on a swipe gesture event. But I always get 0 for both properties, no matter how I do the gesture in the device.
How can I know if the fingers getting closer or getting apart in a gesture zoom event?
Thanks
Maria
First off, why are you using Canvas for a mobile app? It's deprecated and you should use something like Group.
Second, you should look at the API. There's a very good example showing what you're trying to do.
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.TransformGestureEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
[SWF(width=320, height=460, frameRate=24, backgroundColor=0x000000)]
public class TransformGestureExample2 extends Sprite
{
[Embed(source="african_elephant.jpg")]
public var ElephantImage:Class;
public var scaleDebug:TextField;
public var rotateDebug:TextField;
public function TransformGestureExample2()
{
// Debug
var tf:TextFormat = new TextFormat();
tf.color = 0xffffff;
tf.font = "Helvetica";
tf.size = 11;
this.scaleDebug = new TextField();
this.scaleDebug.width = 310;
this.scaleDebug.defaultTextFormat = tf;
this.scaleDebug.x = 2;
this.scaleDebug.y = 2;
this.stage.addChild(this.scaleDebug);
this.rotateDebug = new TextField();
this.rotateDebug.width = 310;
this.rotateDebug.defaultTextFormat = tf;
this.rotateDebug.x = 2;
this.rotateDebug.y = 15;
this.stage.addChild(this.rotateDebug);
var elephantBitmap:Bitmap = new ElephantImage();
var elephant:Sprite = new Sprite();
elephant.addChild(elephantBitmap);
elephant.x = 160;
elephant.y = 230;
elephantBitmap.x = (300 - (elephantBitmap.bitmapData.width / 2)) * -1;
elephantBitmap.y = (400 - (elephantBitmap.bitmapData.height / 2)) *-1;
this.addChild(elephant);
Multitouch.inputMode = MultitouchInputMode.GESTURE;
elephant.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
elephant.addEventListener(TransformGestureEvent.GESTURE_ROTATE, onRotate);
}
private function onZoom(e:TransformGestureEvent):void
{
this.scaleDebug.text = (e.scaleX + ", " + e.scaleY);
var elephant:Sprite = e.target as Sprite;
elephant.scaleX *= e.scaleX;
elephant.scaleY *= e.scaleY;
}
private function onRotate(e:TransformGestureEvent):void
{
var elephant:Sprite = e.target as Sprite;
this.rotateDebug.text = String(e.rotation);
elephant.rotation += e.rotation;
}
}
}