Local variable`s declaration hided field which should have stored brick-spirteHello
I am writing little game from scratch. Everything went ok till i met big problem:
/**
* @(#)GameFromScratch.java
*
* GameFromScratch application
*
* @author #cx3#
* @version 1.00 2015/3/7
*/ import org.jsfml.graphics.RenderWindow;import org.jsfml.graphics.Text;import org.jsfml.graphics.*;import org.jsfml.system.Clock;import org.jsfml.system.Time;import org.jsfml.window.VideoMode;import org.jsfml.window.WindowStyle;import org.jsfml.window.event.Event; import org.jsfml.graphics.Color; import org.jsfml.graphics.Font;import org.jsfml.graphics.Texture;import org.jsfml.graphics.Sprite;import org.jsfml.system.*;import org.jsfml.system.Time;import org.jsfml.system.Vector2f;import org.jsfml.window.Keyboard;import org.jsfml.window.event.Event;import java.util.HashMap;import java.util.Map;import java.util.ArrayList;import java.io.IOException;import java.util.HashMap;import java.nio.file.Paths;import java.io.FileInputStream;import java.io.File;import java.nio.file.*;import java.util.ArrayList;import java.io.IOException;import java.io.*;import java.nio.file.Paths;import java.io.FileInputStream;import java.lang.Number;/**
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/enum KAction
{ left,right,up,down,none
};class INFO
{ public static String MEDIA_DIR
; public static Bricks BRICKS
; public static RenderWindow GAMEWINDOW
;}/**
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/interface IFramePart
{ public Sprite
[] getSprites
();} class ImageLoader
{ Texture texture
= new Texture
(); Sprite sprite
= new Sprite
(); Image image
= new Image(); String name
; int x,y
; private ImageLoader
() { } public ImageLoader
(String spritePath,
String name
) { this.
name = name
; try { texture.
loadFromFile(Paths.
get(spritePath
)); texture.
setRepeated(true); sprite
= new Sprite
(texture
); image
= texture.
copyToImage(); } catch (Exception e
) { e.
printStackTrace(); } } /*
public ImageLoader copy()
{
ImageLoader ret = new ImageLoader();
ret.texture = new Texture(this.texture);
//ret.texture = this.texture;
ret.sprite = new Sprite();
ret.sprite = this.sprite;
ret.image = new Image();
ret.image = this.image;
ret.x = this.x;
ret.y = this.y;
return ret;
}*/ public Sprite copySprite
() { Texture tx
= new Texture
(texture
); tx.
setRepeated(true); return new Sprite
(sprite.
getTexture()); } public void setX
(int x
){ this.
x = x
;} public void setY
(int y
){ this.
y = y
;} public void setXY
(int x,
int y
) { setX
(x
); setY
(y
); } public Sprite getSprite
() { sprite.
setPosition(x,y
); return sprite
; } public Image getImage
() { return image
; } public Texture getTexture
() { return texture
; }} /**
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/ class Bricks
implements IFramePart
{ ArrayList
<Sprite
> bricks
;// = new ArrayList<Vector2i>(); ImageLoader image
; Texture texture
; /**
* IMO here problem is
*/ public Bricks
(String imgPath,
String mapFile
) { image
= new ImageLoader
("c:\\PROJ\\MEDIA\\Textures\\player.png",
"bricks"); image.
setXY(300,
300); Sprite s
= new Sprite
(image.
getTexture()); s.
setPosition(300,
300); bricks
= new ArrayList
<Sprite
>(); bricks.
add(s
); //loadMapFile(mapFile); } private void loadMapFile
(String map
) { } @Override
public Sprite
[] getSprites
() { int s
= bricks.
size(); Sprite
[] ret
= new Sprite
[s
]; for (int i
=0; i
<s
; ++i
) { ret
[i
] = bricks.
get(i
); //ret[i].setPosition(points.get(i).x, points.get(i).y); //ret[i].setOrigin(points.get(i).x+20, points.get(i).y+20); //ret[i].setScale(1,1); } return ret
; } } /**
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/class Player
implements IFramePart
{ ImageLoader il
; Vector2i last, vleft, vright, vup, vdown
; /*
* Load player`s setting at beginning
*/ public Player
(ImageLoader loader
) { il
= loader
; il.
setXY(100,
100); last
= new Vector2i
(100,
100); vleft
= new Vector2i
(-1,
0); //unused yet vright
= new Vector2i
(1,
0); vup
= new Vector2i
(0,
1); vdown
= new Vector2i
(0,
-1); } /*
* this code should checks collisions!
*/ public void tryMoveTo
(Vector2i newPosition
) { //in near futture check collisions here il.
setXY(newPosition.
x, newPosition.
y); last
= newPosition
; //return true; } public void nextMove
(Time dt, KAction action
) { if (action
== KAction.
none) return; switch (action
) { case left
: tryMoveTo
(new Vector2i
(last.
x -1, last.
y) ); break; case right
: tryMoveTo
(new Vector2i
(last.
x +1, last.
y) ); break; case up
: tryMoveTo
(new Vector2i
(last.
x, last.
y -1) ); break; case down
: tryMoveTo
(new Vector2i
(last.
x, last.
y +1) ); break; } } @Override
public Sprite
[] getSprites
() { return new Sprite
[]{il.
getSprite()}; } }/**
* Class responsible for a game loop
*/class GameLoop
{ /*
* Internal helper class
*/ private class Animation
{ ArrayList
<Sprite
> list
;// list of sprites to be drawn public Animation
() { list
= new ArrayList
<Sprite
>(); } public void add
(IFramePart toBeDrawn
) //adds by interface { if (toBeDrawn
==null) return; Sprite
[]sprites
= toBeDrawn.
getSprites(); if (sprites
==null || sprites.
length == 0) return; for (Sprite s
: sprites
) list.
add(s
); } public void add
(Sprite
[] sprites
) { for (Sprite s
: sprites
) list.
add(s
); } public void clear
() { list.
clear(); } public void render
(RenderWindow rw
) { //RenderWindow r = INFO.GAMEWINDOW; rw.
clear(); for (Sprite s
: list
) rw.
draw(s
); rw.
display(); list.
clear(); } }/*
* Body of a GameLoop class
*/ Animation anim
; FloatRect bounds
; Player player
; Time timePerFrame
;// = Time.getSeconds(1.0f / 60.0f); RenderWindow window
; Bricks bricks
; String mediaDirPath
; /* IMPLEMENTATION*/ public GameLoop
(String mediaDirPath
) { this.
mediaDirPath = mediaDirPath
; timePerFrame
= Time.
getSeconds(1.0f
/ 60.0f
); bounds
= new FloatRect
(new IntRect
(0,
0,
800,
600)); window
= new RenderWindow
(new VideoMode
(800,
600),
"GameFromScratch"); player
= new Player
(new ImageLoader
(mediaDirPath
+"\\Textures\\player2.png",
"player")); Bricks bricks
= new Bricks
("C:\\PROJ\\Media\\Textures\\player.png",mediaDirPath
+"\\Maps\\map1.txt"); } private KAction getKey
() { for (Event event
: window.
pollEvents()) { if (event.
type == Event.
Type.
CLOSED) window.
close(); if (Keyboard.
isKeyPressed(Keyboard.
Key.
LEFT)) return KAction.
left; if (Keyboard.
isKeyPressed(Keyboard.
Key.
RIGHT)) return KAction.
right; if (Keyboard.
isKeyPressed(Keyboard.
Key.
UP)) return KAction.
up; if (Keyboard.
isKeyPressed(Keyboard.
Key.
DOWN)) return KAction.
down; } return KAction.
none; } public void play
() { Clock clock
= new Clock
(); Time lastUpdate
= Time.
ZERO; window
= new RenderWindow
(new VideoMode
(800,
600),
"SFML"); anim
= new Animation
(); while(window.
isOpen()) { Time dt
= clock.
restart(); lastUpdate
= Time.
add(lastUpdate, dt
); //window.clear(); while (lastUpdate.
asMicroseconds() > timePerFrame.
asMicroseconds()) { lastUpdate
= Time.
sub(lastUpdate, timePerFrame
); player.
nextMove(lastUpdate, getKey
()); } anim.
add(player
); anim.
add(bricks
); anim.
render(window
); } }} /**
* Run
*/ public class GameFromScratch
{ public static void main
(String[] args
) { INFO.
MEDIA_DIR = "C:\\PROJ\\Media"; GameLoop game
= new GameLoop
(INFO.
MEDIA_DIR); game.
play(); }} Can anybody explain me what i did wrong? Player is displayed and can be moved. I want to make some wall, bricks (name does not matter), but it does not want to be drawn. Why?