I've been using Simple to try to read in my XML file to this class. I really don't know if I've annotated the classes correctly.
I don't know if I need this part:
public Frame()
{
super();
}
public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
{
this.Num = num;
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
this.OffsetX = offsetx;
this.OffsetY = offsety;
this.Duration = duration;]
What does super() do? Do I need getters/setters? Is what I added getters or setters? Do they call themselves automatically or what?
Here's the full class:
public class SpriteAnimationManag
{
// Animation frame class
#Element(name = "Frame")
public class Frame
{
#Element(name = "Num")
public int Num;
#Element(name = "X")
public int X;
#Element(name = "Y")
public int Y;
#Element(name = "Width")
public int Width;
#Element(name = "Height")
public int Height;
#Element(name = "OffSetX")
public int OffsetX;
#Element(name = "OffSetY")
public int OffsetY;
#Element(name = "Duration")
public float Duration;
public Frame()
{
super();
}
public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
{
this.Num = num;
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
this.OffsetX = offsetx;
this.OffsetY = offsety;
this.Duration = duration;
}
}
// Animaiton class to hold the name and frames
public class Animation
{
#Element(name = "Name")
public String Name;
#Element(name = "FrameRate")
public int FrameRate;//may need elementarray or list???
#Element(name = "Loop")
public boolean Loop;
#Element(name = "Pingpong")
public boolean Pingpong;
#ElementArray(name = "Frames")
public Frame[] Frames;
public Animation()
{
super();
}
public Animation(String name, int framerate, boolean loop, boolean pingpong, Frame[] frames)
{
this.Name = name;
this.FrameRate = framerate;
this.Loop = loop;
this.Pingpong = pingpong;
this.Frames = frames;
}
}
// The Sprite Texture stores the Sprite Sheet path.fr
public class SpriteTexture
{
// The Sprite Sheet texture file path
#Element(name = "path")
public String Path;
public SpriteTexture()
{
super();
}
public SpriteTexture(String path)
{
this.Path = path;
}
}
// Aniamtion Set contains the Sprite Texture and Animaitons.
#Root(name = "Animations")
public static class XNAAnimationSet
{
// The sprite texture object
#Element(name = "Texture")
public SpriteTexture SpriteTexture;
// The animation array in the Animation Set
#ElementArray(name = "Animation")
public Animation[] Animations;
public XNAAnimationSet()
{
super();
}
public XNAAnimationSet(SpriteTexture spritetexture, Animation[] animations)
{
this.SpriteTexture = spritetexture;
this.Animations = animations;
}
}
// Sprite Animation Manager class
public final static class SpriteAnimationManager
{
private static final String XNAAnimationSet = null;//was static private static
public static int AnimationCount;
// Read the Sprite Sheet Description information from the description xml file
public static XNAAnimationSet Read(String filename) throws Exception
{
XNAAnimationSet animationSet = new XNAAnimationSet();
Serializer serializer = new Persister();
try {
animationSet = serializer.read(XNAAnimationSet.class, filename );
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Count the animations to Animation Count
AnimationCount = animationSet.Animations.length;
return animationSet;
}
}
}
I've been trying to see what's being read by trying to write the class to a file. The file is created but it's empty.
Can someone tell me if I've annotated this correctly? What am I doing wrong?
I was using jaxb there the last day to parse my xml, I'm not sure how similiar it is to the way your doing it but ill mention a few of the things i needed:
firstly, i think i needed a no-arg constructor in my class, which for you would just be -
public Frame(){};
I believe you do need getters, what you've got there arent getters/setters, your just declaring variables, this really is fundamental java stuff so it might be worth a read up on that before you continue.
When you have your getters defined properly, you then put the #XMLElement annotation above each of them, not above your variable declarators.
A getter looks like:
#XMLElement
public string getName(){ return this.Name};
Also id recommend trying to parse one class at a time, you have multiple inner classes here which i'd imagine gets messy when your trying to parse, i think you need to have #RootElement above the class name declarator, so the xml knows what type of object your creating.
Anyway, there's a few things off the top of my head, best of luck with it!
Related
I have a Util class which is capable of creating color codes(decimal).
public class ColorUtils {
private static final String RED = "ff0000";
private static final String GREEN = "00ff00";
private static final String BLUE = "0000ff";
private static final String WHITE = "ffffff";
private static final int RADIX = 16;
public static int getColorShade(String deepShade, String lightShade, float percentage) {
if (percentage > 100) {
throw new RuntimeException("Percentage can not be more than 100");
}
int deepShadeCode = Integer.parseInt(deepShade, RADIX);
int lightShadeCode = Integer.parseInt(lightShade, RADIX);
int shadeDifference = deepShadeCode - lightShadeCode;
int shadeOffset = (int) (shadeDifference * percentage)/100;
return lightShadeCode + shadeOffset;
}
public static int getColorShade(String deepShade, float percentage) {
return getColorShade(deepShade, WHITE, percentage);
}
public static int getRedColorShade(float percentage) {
return getColorShade(RED, percentage);
}
public static int getGreenColorShade(float percentage) {
return getColorShade(GREEN, percentage);
}
public static int getBlueColorShade(float percentage) {
return getColorShade(BLUE, percentage);
}
public static int getWhite() {
return Integer.parseInt(WHITE, RADIX);
}
}
Is there any way to convert it to Android color?
All I could find out is ContextCompat.getColor(this,R.color.yourcolor)
but this will take resource id in second arguments, I don't want to make color pallets in color.xml. Is there a work around?
Don't know how you create the color, but if it can extract red, green, blue, then try this:
#ColorInt int color = Color.rgb(getRedColorShade(percentage), getGreenColorShade(percentage), getBlueColorShade(percentage));
Ref here, this time it's pretty sure that this method is added from API level 1 and can be used instead of valueOf
Hi i am developing a snake game and i want to store the snake movement points in a priority queue but i am not getting how to iterate through that priority queue
below is my Priority Queue class and Point class to store points
Priority Queue class:
public class PriorityQueue {
private Points[] heap;
private int heapSize,capacity;
/** Constructor **/
public PriorityQueue(){
heapSize = 0;
}
public void setCapacity( int capacity)
{
this.capacity = capacity + 1;
heap = new Points[this.capacity];
}
/** function to clear **/
public void clear()
{
heap = new Points[capacity];
heapSize = 0;
}
/** function to check if empty **/
public boolean isEmpty()
{
return heapSize == 0;
}
/** function to check if full **/
public boolean isFull()
{
return heapSize == capacity - 1;
}
/** function to get Size **/
public int size()
{
return heapSize;
}
// method to insert point
public void insert(float x, float y, float rotation, int priority)
{
Points points = new Points(x,y,rotation,priority);
heap [++heapSize] = points;
int pos = heapSize;
while (pos != 1 && points.priority > heap[pos/2].priority)
{
heap[pos] = heap[pos/2];
pos /=2;
}
heap[pos] = points;
}
// function to remove point
public Points remove()
{
int parent, child;
Points item, temp;
if (isEmpty() )
{
System.out.println("Heap is empty");
return null;
}
item = heap[1];
temp = heap[heapSize--];
parent = 1;
child = 2;
while (child <= heapSize)
{
if (child < heapSize && heap[child].priority < heap[child + 1].priority)
child++;
if (temp.priority >= heap[child].priority)
break;
heap[parent] = heap[child];
parent = child;
child *= 2;
}
heap[parent] = temp;
return item;
}
}
Point Class:
public class Points {
private float X;
private float Y;
int priority;
private float rotation;
public Points(float x, float y,float rotation,int priority)
{
this.X = x;
this.Y = y;
this.rotation = rotation;
this.priority = priority;
}
public float getX() {
return X;
}
public float getY() {
return Y;
}
public float getRotation() {
return rotation;
}
public int getPriority() {
return priority;
}
}
please guide me to how iterate through this Queue Class.
Thank you in advance
I searched for and wide on the internet and I have yet to find an answer. I have an object called GameIcon which extends Sprite. Everything about it is okay except for the texture. Here is my code for the GameIcon class.
package com.xx4everPixelatedxx.gaterunner.sprites;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.xx4everPixelatedxx.gaterunner.GateRunner;
import javax.xml.soap.Text;
/**
* Created by Michael Jan on 8/17/2015.
*/
public class GameIcon extends Sprite {
private int vX = 3;
private int vY = 3;
private int r = 9;
private int rotation;
private Vector3 position;
private Texture texture;
public GameIcon(int x, int y) {
position = new Vector3(x, y, 0);
texture = new Texture(Gdx.files.internal("icon_players/icon1.png"));
setTexture(texture);
}
public void update() {
position.add(vX, vY, 0);
rotation = rotation + r;
rotation = rotation % 360;
setRotation(rotation);
setOriginCenter();
}
public void addPosition(int x, int y) {
position.add(x, y, 0);
setOriginCenter();
}
public void negateVelocityX() {
vX = -vX;
}
public void negateRotation() {
r = -r;
}
public Vector3 getPosition() {
return position;
}
public int getvY() {
return vY;
}
public void setvY(int vY) {
this.vY = vY;
}
public int getvX() {
return vX;
}
public void setvX(int vX) {
this.vX = vX;
}
}
Here is the icon1.png:
https://i.gyazo.com/1d52f5e58b227f08809f6c14ae4c94a4.png
Here is what I am getting:
https://i.gyazo.com/1881a9392955af34de5c55b9b8fac391.png
Note that the rotation of the image and the particles are intended. The problem is that the big square should be the texture (icon1.png), and I do not know how to fix this.
(Not enough reputation to post pictures)
I'm not familiar with LibGDX, but this may have something to do with the fact that you are maybe overwriting TextureRegion.texture. Could you try to user your parent class Sprite(Texture) constructor like this:
...
public class GameIcon extends Sprite {
private int vX = 3;
private int vY = 3;
private int r = 9;
private int rotation;
private Vector3 position;
//private Texture texture;
public GameIcon(int x, int y) {
super(new Texture(Gdx.files.internal("icon_players/icon1.png")))
position = new Vector3(x, y, 0);
}
...
As stated by Tenfour04 in comments, this method works because the parent's constructor applies the width and height of the texture, while setTexture() does not.
I've been working on the example from http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/ In this I want to make few changes.
Speed.java
public class Speed {
public static final int DIRECTION_RIGHT = 1;
public static final int DIRECTION_LEFT = -1;
public static final int DIRECTION_UP = -1;
public static final int DIRECTION_DOWN = 1;
private float xv = 1; // velocity value on the X axis
private float yv = 1; // velocity value on the Y axis
private int xDirection = DIRECTION_RIGHT;
private int yDirection = DIRECTION_DOWN;
public Speed() {
this.xv = 1;
this.yv = 1;
}
public Speed(float xv, float yv) {
this.xv = xv;
this.yv = yv;
}
public float getXv() {
return xv;
}
public void setXv(float xv) {
this.xv = xv;
}
public float getYv() {
return yv;
}
public void setYv(float yv) {
this.yv = yv;
}
public int getxDirection() {
return xDirection;
}
public void setxDirection(int xDirection) {
this.xDirection = xDirection;
}
public int getyDirection() {
return yDirection;
}
public void setyDirection(int yDirection) {
this.yDirection = yDirection;
}
// changes the direction on the X axis
public void toggleXDirection() {
xDirection = xDirection * -1;
}
// changes the direction on the Y axis
public void toggleYDirection() {
yDirection = yDirection * -1;
}
}
Using this, the image moves in all directions. Now I just want to limit this movement from bottom to top. And the functionality for onclick is that, we can click and drag the image to required position. I want to replace that with just make the image to disappear or go to another activity. Please help me in making changes to this code. Thanks in advance.
if you have used SurfaceView then in onDraw(Canvas canvas) method the code is for moving image from bottom to top is something like this.
canvas.drawBitmap(bitmap,xPoint,yPoint,paint);
where bitmap is an image(Bitmap which you want to move),xPoint is the x coordinate,yPoint is the y coordinate and paint is a Paint which is also can be null.
and for bottom to top movement just update
yPoint = yPoint - 1;
in any thread before onDraw() call.
May this help you.
< want to make a parcelable from an Object that contains an Arraylist, But in my readFromParcel method I get the error Type Mismatch: cannot convert from void to ArrayList. What can I do to read my ArrayList properly from my parcel?
edit: With the help of the answers below I now no longer get a type mismatch error, but now I get the message"- Syntax error on token ">", invalid Name - Syntax error on token ">", Expression expected after this token"
edit New errors were resolved when I cleaned the project.
Here's my code
public class Game implements Parcelable{
private ArrayList<Stone> allStones;
public Game(){
allStones = new ArrayList<Stone>();
for(int x=0; x<10; x++) {
for(int y=0; y<10; y++) {
if((x+y)%2 == 1 && y<4){
Stone stone = new Stone(x, y, Stone.WHITE);
allStones.add(stone);
} else if((x+y)%2 == 1 && y>5){
Stone stone = new Stone(x, y, Stone.BLACK);
allStones.add(stone);
}
}
}
}
public Game(Parcel in) {
allStones = new ArrayList<Stone>();
readFromParcel(in);
}
public ArrayList<Stone> getAllStones() {
return allStones;
}
public void removeFromStones(Stone stone) {
allStones.remove(stone);
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(allStones);
}
private void readFromParcel(Parcel in) {
in.readTypedList(allStones, Stone.CREATOR); //This line has the error in it
}
}
And the Stone class
public class Stone implements Parcelable{
private int x, y, color;
private Boolean king;
public static final int BLACK = 0;
public static final int WHITE = 1;
public Stone(int x, int y, int color) {
this.x = x;
this.y = y;
this.color = color;
this.king = false;
}
public Stone(Parcel in) {
readFromParcel(in);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getColor() {
return color;
}
public boolean getKing() {
return king;
}
public void setKing() {
king = true;
}
public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(x);
dest.writeInt(y);
dest.writeInt(color);
dest.writeByte((byte) (king ? 1:0));
}
public void readFromParcel(Parcel in) {
x = in.readInt();
y = in.readInt();
color = in.readInt();
king = in.readByte() == 1;
}
public final static Creator<Stone> CREATOR = new Parcelable.Creator<Stone>() {
public Stone createFromParcel(Parcel source) {
return new Stone(source);
}
public Stone[] newArray(int size) {
return new Stone[size];
}
};
}
readTypedList() does not return a value. It puts the list of objects into the list you pass as the first parameter. You code should look like this:
private void readFromParcel(Parcel in) {
in.readTypedList(allStones, Stone.CREATOR); // Should work now
}