vendredi 30 janvier 2015

Builder pattern not working

So I am currently working on a game in Java with the help of LibGDX. Recently I bumped into a problem, when tryin to make a Builder class for another class which will have a lot of parameters, so I guessed it needs a Builder class. Being the confident java programmer I am, I started it and I found a problem which I can't correct. I have already looked it up on the internet and found some examples, but everything seems OK for me. Maybe you can help. The code needed:



public class SpecialTile implements MyAnimation {

/**
* Class builder for SpecialTile
* @author Zsemberi Daniel
*
*/
public static class SpecialTileBuilder {
/*
* Drawables
*/
private Sprite image;
private Animation anim;

//position
private Point position;

/**
* Sprite path constructor
*/
public SpecialTileBuilder(String imagePath) {
this.image = new Sprite((Texture) Load.manager.get(imagePath));
}

/**
* Image constructor
*/
public SpecialTileBuilder(Sprite image) {
this.image = image;
}

/**
* Animation constructor
*/
public SpecialTileBuilder(Animation anim) {
this.anim = anim;
}

//Set position
public SpecialTileBuilder setPosition(Point position) {
this.position = position;
return this;
}


public SpecialTile createSpecialTile() {
return new SpecialTile(this);
}
}

/*
* Drawables
*/
private Sprite image;
private Animation anim;

private float stateTime = 0f;

//position
private Point position;

protected SpecialTile(SpecialTileBuilder builder) {
image = builder.image;
anim = builder.anim;
position = builder.position;
}

/**
* Hits the player
*/
public boolean isHit(Sprite sprite) {
if(anim == null)
return image.getBoundingRectangle().contains(sprite.getBoundingRectangle());
else
return new Sprite(getCurrentFrame()).getBoundingRectangle().contains(sprite.getBoundingRectangle());
}

/**
* If it is triggered it does something
*/
public void doTheHarlemShake(Sprite sprite) {

}

public Sprite getSprite() { return image; }
public Animation getAnimation() { return anim; }

public float getX() { return position.x; }
public float getY() { return position.y; }

/*
* Animation stuff
*/

@Override
public TextureRegion getCurrentFrame() {
return anim.getKeyFrame(stateTime, true);
}

@Override
public void updateAnimation(float delta) {
stateTime += delta;
}

}


So up there you can see the whole class (I thought you may need it) where I wrote the Buidler. And here comes how I would use it if it worked.



SpecialTile.SpecialTileBuilder("test.png")
.setPosition(new Point(2, 2))
.createSpecialTile();


So it says in eclipse that I have got this problem:



The method SpecialTileBuilder(String) is undefined for the type SpecialTile



Aucun commentaire:

Enregistrer un commentaire