I have created a 10x10 grid, and now would like to place an image of the player inside one of the grid squares. to do this I think i have to use bitmap? I will put all my code below. to try and do this anyway I created a game class which sets and gets the x position of the player image. I then created a player class, which is where I try to add the image, using Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1);
i keep getting an error under getResources so i created a get resources method in game, but it didnt fix the problem. could someone please show me how to do this, my code is below.
//DRAW CLASS
public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();
int rectSide = 1000;
public Draw(Context context) {
super(context);
red.setColor(Color.RED);
green.setColor(Color.GREEN);
red.setStrokeWidth(8);
green.setStrokeWidth(8);
}
public void drawGrid(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
float startX = (width / 2) - (rectSide / 2);
float stopX = (width / 2) + (rectSide / 2);
float startY = (height / 2) - (rectSide / 2);
float stopY = (height / 2) + (rectSide / 2);
for (int i = 0; i < 1100; i += 100) {
float newY = startY + i;
canvas.drawLine(startX, newY, stopX, newY, green);
}
for (int i = 0; i < 1100; i += 100) {
float newX = startX + i;
canvas.drawLine(newX, startY, newX, stopY, green);
}
}
#Override
public void onDraw(Canvas canvas) {
drawGrid(canvas);
}
}
//GAME CLASS
public class Game {
Bitmap image;
int x;
int y;
int height;
int width;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public Bitmap getResources(){
return image;
}
}
//PLAYER CLASS
public class Player extends Game {
public Player(Bitmap res, int w, int h){
image = res;
x = 300;
y = 300;
height = h;
width = w;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1);
}
}
//MAIN ACTIVITY
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Replace your Player class with this:
//PLAYER CLASS
class Player extends Game {
public Player(Context context, int w, int h){
x = 300;
y = 300;
height = h;
width = w;
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}
}
Related
Hi I currently have a SurfaceView Runnable public class mapsLayout extends SurfaceView implements Runnable { which draws lines based on current geolocation. My previous solution involved having a main activity which contains the Android location manager - using a FusedLocationProviderClient as recommended by the tutorials provided by Google - and then calling the SurfaceView.
mainActivity with Android location manager -> 'SurfaceView'
However this means that the SurfaceView resets every time the location changes which happens every 10000 ticks.
Is there a way for the SurfaceView to contain the Android location manager.
If it helps I have my SurfaceView Runnable code below
public class mapsActivityLayout extends SurfaceView implements Runnable {
MyThread draw = null;
boolean canDraw = false;
Bitmap map;
SurfaceHolder surfaceHolder;
Context mContext;
Paint paint;
ArrayList<String> endNodes = new ArrayList<String>();
int bitmapX;
int bitmapY;
int viewWidth;
int viewHeight;
Paint red_paintbrush_fill, blue_paintbrush_fill, green_paintbrush_fill;
Paint red_paintbrush_stroke, blue_paintbrush_stroke, green_paintbrush_stroke;
Path line;
Path circle;
public mapsActivityLayout(Context context, ArrayList<String> endNodes) {
super(context);
this.endNodes = endNodes
mContext = context;
surfaceHolder = getHolder();
paint = new Paint();
paint.setColor(Color.DKGRAY);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
draw = new MyThread(viewWidth, viewHeight);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inMutable = true;
map = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mapimage, options);
setUpBitmap();
}
#Override
public void run() {
Canvas canvas;
prepPaintBrushes();
while (canDraw) {
//draw stuff
if (surfaceHolder.getSurface().isValid()) {
int x = draw.getX();
int y = draw.getY();
//int radius = draw.getRadius();
canvas = surfaceHolder.lockCanvas();
canvas.save();
canvas.drawBitmap(map, bitmapX, bitmapY, paint);
ArrayList<Double> currentLocation = convertGeoToPixel(currentLat, currentLon);
ArrayList<Double> destination = convertGeoToPixel(destinationLat, destinationLat);
int width = 2699;
int height = 2699;
canvas.drawCircle(currentX, currentY, 20, red_paintbrush_fill);
canvas.drawCircle(destX, destY, 20, green_paintbrush_fill);
path.rewind();
canvas.restore();
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
private void updateFrame(int newX, int newY) {
draw.update(newX, newY);
}
/**
* Calculates a randomized location for the bitmap
* and the winning bounding rectangle.
*/
private void setUpBitmap() {
bitmapX = (int) Math.floor(
Math.random() * (viewWidth - backGround.getWidth()));
bitmapY = (int) Math.floor(
Math.random() * (viewHeight - backGround.getHeight()));
}
public void pause() {
canDraw = false;
while (true) {
try {
thread.join();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void resume() {
canDraw = true;
thread = new Thread(this);
thread.start();
}
private void prepPaintBrushes() {
red_paintbrush_fill = new Paint();
red_paintbrush_fill.setColor(Color.RED);
red_paintbrush_fill.setStyle(Paint.Style.FILL);
green_paintbrush_stroke = new Paint();
green_paintbrush_stroke.setColor(Color.GREEN);
green_paintbrush_stroke.setStyle(Paint.Style.STROKE);
green_paintbrush_stroke.setStrokeWidth(10);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setUpBitmap();
updateFrame((int) x, (int) y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
updateFrame((int) x, (int) y);
invalidate();
break;
default:
}
return true;
}
}
thread code
public class MyThread extends Thread {
private int mX;
private int mY;
public MyThread(int viewWidth, int viewHeight) {
//super()
mX = viewWidth / 2;
mY = viewHeight / 2;
}
/**
* Update the coordinates of the map.
*
* #param newX Changed value for x coordinate.
* #param newY Changed value for y coordinate.
*/
public void update(int newX, int newY) {
mX = newX;
mY = newY;
}
public int getX() {
return mX;
}
public int getY() {
return mY;
}
}
I just want to say I am very new to java app development.
So I have this image floating around the screen and when I touch the screen I want the image to change colors (switch from image to image1)
I got this working but the image gets moved to starting point, I want the transition to be smooth and to take place where the image is at certain point.
I have this code in my GameView.java class
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;
private CharacterSprite characterSprite;
private int col = 0;
public GameView(Context context){
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(),this);
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder){
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen), 100, 100);
thread.setRunning(true);
thread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(retry){
try{
thread.setRunning(false);
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
retry = false;
}
}
public void update(){
characterSprite.update();
}
public boolean onTouchEvent(MotionEvent event) {
int X = (int) event.getX();
int Y = (int) event.getY();
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
if(col == 0){
int ox = characterSprite.getX();
int oy = characterSprite.getY();
characterSprite.setX(ox);
characterSprite.setX(oy);
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen1), ox, oy);
Toast.makeText(getContext(), "ACTION_UP "+"X: "+ox+" Y: "+oy, Toast.LENGTH_LONG).show();
col = 1;
}else{
int ox = characterSprite.getX();
int oy = characterSprite.getY();
characterSprite.setX(ox);
characterSprite.setX(oy);
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen), ox, oy);
Toast.makeText(getContext(), "ACTION_UP "+"X: "+ox+" Y: "+oy, Toast.LENGTH_LONG).show();
col = 0;
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
#Override
public void draw(Canvas canvas){
super.draw(canvas);
if(canvas!=null){
characterSprite.draw(canvas);
}
}
This is where most of work is taking place, I also have CharacterSprite.java class which basicly creates the 'image' and makes it float around the screen:
public class CharacterSprite {
private Bitmap image;
private int x,y;
private int xVelocity = 10;
private int yVelocity = 5;
private int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
private int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
public CharacterSprite(Bitmap bmp, int x, int y){
image = bmp;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int nx){
x = nx;
}
public void setY(int ny){
y = ny;
}
public void draw(Canvas canvas){
canvas.drawBitmap(image,x,y,null);
}
public void update(){
if(x < 0 && y < 0){
x = screenWidth / 2;
y = screenHeight / 2;
}else{
x += xVelocity;
y += yVelocity;
if((x > screenWidth - image.getWidth()) || (x < 0)){
xVelocity = xVelocity*-1;
}
if((y > screenHeight - image.getHeight()) || (y < 0)){
yVelocity = yVelocity*-1;
}
}
}
as You can see I tried to get current x and y and set it back when creating new character, but that did not work, my guess is that this is happening because for a second the image is not on the screen and the whole thing reset but 1. I am not sure if thats right 2. I have no idea how I could fix that.
If anyone could point me where I should search that would be awesome.
#edit:
I changed to this Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
To debug and the output I am getting is ACTION_UP X: 0 Y: 0
ANSWER
Added
public void updateD(Canvas canvas){
canvas.drawBitmap(image,x,y,null);
}
To CharacterSprite.java and changed:
if(col == 0){
int ox = 550; //characterSprite.getX();
int oy = 550;//characterSprite.getY();
// characterSprite.setX(ox);
//characterSprite.setX(oy);
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen1));
Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
col = 1;
}
To:
if(col == 0){
characterSprite.setImg(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen1));
Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
col = 1;
}
I was trying to animate a sprite for an android app and i've encounter a small problem. I'm drawing on a surfaceView that i add on top of already existing layouts. On this surfaceView, i wanted to animate few sprites so they would walk along a path.
So this is the result i'm facing right now :
The walking sprite is leaving a trail. So i decided to google this problem and apparently i had to clear the canvas first before drawing on it.
This was my onDraw method before :
public void onDraw(Canvas canvas) {
update();
int srcX = currentFrame * width;
int srcY = directionX * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
And this was my onDraw method after
public void onDraw(Canvas canvas) {
canvas.drawRGB(0, 0, 0);
update();
int srcX = currentFrame * width;
int srcY = directionX * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
So i now have this as a result
which is great because it leaves no more trail BUT i cant see the layouts underneath. Is there anyway i can get the best of both world ? I thought clearing the canvas would work but it obviously doesnt not work as expected.
I'll post my code below
SurfaceView :
public class MonsterView extends SurfaceView {
private Bitmap monsterImg;
private SurfaceHolder holder;
private MonsterThread mainThread;
private Sprite monsterSprite;
private int x;
private int xSpeed = 1;
public MonsterView(Context context) {
super(context);
this.mainThread = new MonsterThread(this);
this.x = 0;
holder = getHolder();
holder.setFormat(PixelFormat.TRANSPARENT);
holder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
mainThread.setRunning(true);
mainThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mainThread.setRunning(false);
while (retry) {
try {
mainThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
});
setZOrderOnTop(true);
monsterImg = BitmapFactory.decodeResource(getResources(), R.drawable.fire);
monsterSprite = new Sprite(this,monsterImg);
}
#Override
public void onDraw(Canvas canvas) {
if (x == getWidth() - monsterImg.getWidth()) {
xSpeed = -1;
}
if (x == 0) {
xSpeed = 1;
}
x = x + xSpeed;
monsterSprite.onDraw(canvas);
}
Thread :
public class MonsterThread extends Thread {
private MonsterView monsterSurface;
private boolean running;
static final long FPS = 35;
public MonsterThread(MonsterView monsterSurface){
this.monsterSurface = monsterSurface;
this.running = false;
}
#Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while(running){
Canvas c = null;
startTime = System.currentTimeMillis();
try{
c = monsterSurface.getHolder().lockCanvas();
synchronized (monsterSurface.getHolder()){
monsterSurface.onDraw(c);
}
} finally {
if( c!= null){
monsterSurface.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try {
if(sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e){}
}
}
public MonsterView getMonsterSurface() {
return monsterSurface;
}
public void setMonsterSurface(MonsterView monsterSurface) {
this.monsterSurface = monsterSurface;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
Sprite :
public class Sprite {
private static final int BMP_ROWS = 4;
private static final int BMP_COLUMNS = 3;
private int x = 0;
private int y = 0;
private int xSpeed = 5;
private MonsterView monsterView;
private Bitmap bmp;
private int currentFrame = 0;
private int width;
private int height;
private int directionX;
public Sprite(MonsterView monsterView, Bitmap bmp) {
this.monsterView = monsterView;
this.bmp=bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
this.directionX = 2;
}
private void update() {
if (x > monsterView.getWidth() - width - xSpeed) {
xSpeed = -5;
directionX = 1;
}
if (x + xSpeed < 0) {
xSpeed = 5;
directionX = 2;
}
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
Log.d("test", ""+currentFrame);
}
public void onDraw(Canvas canvas) {
update();
int srcX = currentFrame * width;
int srcY = directionX * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
You're using setZOrderOnTop(), which is putting the Surface part of the SurfaceView on a layer above everything else. (By default, it's a separate layer below everything else.) When you clear it with canvas.drawRGB(0, 0, 0) you're setting the entire layer to opaque black, which is going to obscure the View layer beneath it.
If instead you clear it to transparent black, with canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR), you should get the results you want.
FWIW, you shouldn't override onDraw() if you're only drawing on the Surface. The onDraw() method is called by the View hierarchy to draw on the View part of the SurfaceView. If something manages to invalidate the SurfaceView's View, your onDraw() will be called, and you'll end up with a character sprite on the View layer. (Depending on your layout this may not be visible.) Just give the method a different name.
There are a number of ways to handle this, but generally, the way to do this is by maintaining a background layer (which is just another image), and sprite layers. Instead of clearing, at each frame you blit (copy) your background onto the surface (which erases everything), then you blit your sprites.
So you're going to have to draw the background bitmap onto the surface first before drawing your sprite. Right now you're drawing black which is covering your background layouts.
Alternatively you might be able to do canvas.drawRect with a Paint with paint.setColor to Color.Transparent.
public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private int x = 0;
private int y = 0;
private int xSpeed = 1;
public static int WIDTH = 800;
public static int HEIGHT = 450;
private Background bg;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.tuo));
bg.setVector(-5);
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic);
// tutaj dorysować tło
}
#Override
protected void onDraw(Canvas canvas) {
if (x == getWidth() - bmp.getWidth()) {
xSpeed = -4;
}
if (x == 0) {
xSpeed = 4;
}
x = x + xSpeed;
final float scaleFactorX = getWidth()/WIDTH;
final float scaleFactorY = getHeight()/HEIGHT;
if(canvas!=null) {
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);
bg.draw(canvas);
canvas.restoreToCount(savedState);
}
canvas.drawBitmap(bmp, x , 10, null);
}
public void update()
{
bg.update();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x=x+10;
return super.onTouchEvent(event);
}
}
I can't scale the image - it should make a scale by dividing getWidth/WIDTH (and height too) - WIDTH and HEIGHT are the image dimensions.
It just prints a left, upper corner of an image, like there is totally no scaling ;(
If someone have any idea, I would be thankful :)
u need to calc:
/*
WIDTH/getWidth() = 4; // if ur image width = 200px
scaleFactorX = 4*getwidth();
scaleFactorY = 4*getHeight();
*/
final float scaleFactorX = WIDTH/getWidth();
final float scaleFactorY = HEIGHT/getHeight();
canvas.scale(scaleFactorX*getWidth(), scaleFactorY*getHeight());
i hope they are proportionaly...
1600*900 or something like this... 400*225
Whenever I try to draw a ninepatch image and a stage the last thing that's called is being drawn. I have tried to use a orthographic camera but didn't succeed. What I have tried:
batch.setProjectionMatrix(camera.combined);
ninePatch.draw(batch, xPos, yPos, width, height);
stage.act(delta);
stage.draw();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.setCamera(camera)
EDIT: The code
The CreateQuiz class
public class CreateQuiz implements Screen{
public CreateQuiz(Quiz quiz){
this.quiz = quiz;
}
private Quiz quiz;
private FallDownPanel fallDownPanel;
private OrthographicCamera camera;
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
quiz.beginBatch();
fallDownPanel.render(quiz.getSpriteBatch(), delta);
quiz.endBatch();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
fallDownPanel = new FallDownPanel(FallDownPanel.START_LOWER_SIDE, 200, quiz.getTextureAtlas().createPatch("fallDownWindow"));
Stage stage = new Stage(fallDownPanel.getWidth(), fallDownPanel.getHeight(), false);
TextFieldStyle style = quiz.getGreenTextFieldStyle();
style.font.scale(-0.30f);
TextFieldQuiz test = new TextFieldQuiz("Hej åäö 123 !", style, 0, 2, 400, 16);
test.setTextFieldListener(new TextFieldListener() {
#Override
public void keyTyped (TextField textField, char key) {
if (key == '\n') {
textField.getOnscreenKeyboard().show(false);
}
}
});
stage.addActor(test);
Gdx.input.setInputProcessor(stage);
fallDownPanel.setStage(stage);
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage.setCamera(camera);
fallDownPanel.setCamera(camera);
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
The FallDownPanel.class
public class FallDownPanel {
//With repeat
public FallDownPanel(int startSide, int size, NinePatch ninePatch){
Tween.registerAccessor(FallDownPanel.class, new FallDownPanelTween());
Tween.registerAccessor(Widget.class, new WidgetTween());
tweenManager = new TweenManager();
final int screenWidth = Gdx.graphics.getWidth();
final int screenHeight = Gdx.graphics.getHeight();
int target = 0;
int tweenType = 0;
if(startSide == START_LEFT_SIDE){
setSize(size, screenHeight);
xPos = -size;
yPos = 0;
target = 0;
tweenType = FallDownPanelTween.POSITION_X;
}
else if(startSide == START_RIGHT_SIDE){
setSize(size, screenHeight);
xPos = screenWidth;
yPos = 0;
target = screenWidth - size;
tweenType = FallDownPanelTween.POSITION_X;
}
else if(startSide == START_UPPER_SIDE){
setSize(screenWidth, size);
xPos = 0;
yPos = screenHeight + size;
target = screenHeight - size;
tweenType = FallDownPanelTween.POSITION_Y;
}
else if(startSide == START_LOWER_SIDE){
setSize(screenWidth, size);
xPos = 0;
yPos = -size;
target = 0;
tweenType = FallDownPanelTween.POSITION_Y;
}
Tween.to(this, tweenType, 1).target(target).start(tweenManager);
this.tweenType = tweenType;
this.startSide = startSide;
this.ninePatch = ninePatch;
}
private TweenManager tweenManager;
private NinePatch ninePatch;
private Stage stage;
private OrthographicCamera camera;
private float xPos, yPos;
private int width, height;
private int tweenType;
private int startSide;
public static final int START_LEFT_SIDE = 0, START_RIGHT_SIDE = 1, START_UPPER_SIDE = 2, START_LOWER_SIDE = 3;
public void render(SpriteBatch batch, float delta){
tweenManager.update(delta);
batch.setProjectionMatrix(camera.combined);
ninePatch.draw(batch, xPos, yPos, width, height);
stage.act(delta);
stage.draw();
}
public void setX(float x){
this.xPos = x;
}
public void setY(float y){
this.yPos = y;
}
public float getX(){
return xPos;
}
public float getY(){
return yPos;
}
public float getWidth(){
return width;
}
public float getHeight(){
return height;
}
private void setSize(int w, int h){
width = w;
height = h;
}
public Stage getStage() {
return stage;
}
public void setStage(Stage stage) {
this.stage = stage;
startWidgetTweens();
}
private void startWidgetTweens(){
float size = getShortestSide();
Array<Actor> actors = stage.getActors();
for(int i = 0; i < actors.size; i++){
Widget w = (Widget) actors.get(i);
Tween.to(w, tweenType, 1).target(0).start(tweenManager);
}
}
private float getShortestSide() {
if(startSide == START_LEFT_SIDE){
return width;
}
else if(startSide == START_RIGHT_SIDE){
return width;
}
else if(startSide == START_UPPER_SIDE){
return height;
}
else if(startSide == START_LOWER_SIDE){
return height;
}
return -1;
}
public void setCamera(OrthographicCamera camera) {
this.camera = camera;
}
}
Always make sure you have only one active Renderer at a time. The Renderers are:
Batch
SpriteBatch
ShapeRenderer
Box2DDebugRenderer if i am not wrong
ModelBatch for 3D
I hope i did not forget one.
Make sure you always call end() for the active one, before calling begin() for a new one.
Also remember, that a Stage has its own SpriteBatch and it calls begin() for this SpriteBatch, if you call stage.draw(). So make sure you end() the active Renderer, before stage.draw().
If you need a ShapeRenderer or any other renderer to draw an Actor (maybe cause of debuging), you have to end the stages SpriteBatch, which you get with the draw() method. Make sure you begin() it again at the end of the method.
Hope this is clear enough.