ricardoneves93 Posted August 5, 2013 at 09:28 PM Report #520913 Posted August 5, 2013 at 09:28 PM Boa noite, estou com um problema em ler um ficheiro xml para a construção de níveis de jogo. O curioso é que eu tentei em java sem ser pelo ADT e funcionou, mas no ADT diz sempre que nao consegue abrir já tentei colocar em vários directórios e nada. já coloquei na raiz do disco C e também já coloquei numa pasta dentro do eclipse e nada. public class ReadXMLFile extends DefaultHandler{ Rect[] objs = new Rect[2]; String xPosS, yPosS, xSizeS, ySizeS; float xPosI, yPosI, xSizeI, ySizeI; int counter = 0; Rect[] ParseFile() { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean xPos = false; boolean yPos = false; boolean xSize = false; boolean ySize = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //System.out.println("Start Element :" + qName); if (qName.equalsIgnoreCase("XPOS")) { xPos = true; } if (qName.equalsIgnoreCase("YPOS")) { yPos = true; } if (qName.equalsIgnoreCase("XSIZE")) { xSize = true; } if (qName.equalsIgnoreCase("YSIZE")) { ySize = true; } } public void endElement(String uri, String localName, String qName) throws SAXException { } public void characters(char ch[], int start, int length) throws SAXException { if (xPos) { xPosS = new String(ch, start, length); xPosI = Float.valueOf(xPosS); xPos = false; } if (yPos) { yPosS = new String(ch, start, length); yPosI = Float.valueOf(yPosS); yPos = false; } if (xSize) { xSizeS = new String(ch, start, length); xSizeI = Float.valueOf(xSizeS); xSize = false; } if (ySize) { ySizeS = new String(ch, start, length); ySizeI = Float.valueOf(ySizeS); ySize = false; ConstructRectangle(xPosI, yPosI, xSizeI, ySizeI); } } private void ConstructRectangle(float xPosI, float yPosI, float xSizeI, float ySizeI) { // TODO Auto-generated method stub objs[counter] = new Rect(xPosI, yPosI, xSizeI, ySizeI); counter++; } }; saxParser.parse("res/xml/file.xml", handler); } catch (Exception e) { e.printStackTrace(); } return objs; } } Obrigado pela atenção. Cumprimentos. http://www.speedtest.net/result/2682095343.png
KTachyon Posted August 5, 2013 at 10:31 PM Report #520919 Posted August 5, 2013 at 10:31 PM Tens que o colocar na pasta assets. Depois utilizas a função getAssets() (http://developer.android.com/reference/android/content/res/Resources.html), que te dá um AssetManager. Daí podes abrir a InputStream para o ficheiro que podes passar directamente para o SAXParser. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
ricardoneves93 Posted August 5, 2013 at 11:01 PM Author Report #520921 Posted August 5, 2013 at 11:01 PM (edited) Só estou com uma duvida, mesmo consultando a documentação não estou a conseguir criar um objecto do tipo Resources. Podes dar umas dicas? não sei o que devo colocar nos campos do construtor. Obrigado pela ajuda 😉 Edited August 5, 2013 at 11:01 PM by ricardoneves93 http://www.speedtest.net/result/2682095343.png
KTachyon Posted August 6, 2013 at 08:41 PM Report #521017 Posted August 6, 2013 at 08:41 PM Podes fazer getAssets() num Context. Uma Activity é um Context, logo se estiveres a fazer essa chamada numa Activity podes simplesmente chamar a função sem precisares de um objecto Resources. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
ricardoneves93 Posted August 6, 2013 at 11:27 PM Author Report #521041 Posted August 6, 2013 at 11:27 PM Mas neste caso não estou numa activity. Estou numa classe separada a única classe que está numa activity é a classe que faz extend á classe BaseGameActivity que pertence ao Andengine, nesta classe consigo fazer o que disseste mas na classe onde o parser está implementado isso é impossível. http://www.speedtest.net/result/2682095343.png
KTachyon Posted August 7, 2013 at 12:56 AM Report #521046 Posted August 7, 2013 at 12:56 AM Passas o Contexto (Activity) para essa classe. Adicionas uma variável Context à tua classe e um construtor: public ReadXMLFile(Context context) { super(); this.context = context; } “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
ricardoneves93 Posted August 7, 2013 at 07:16 PM Author Report #521139 Posted August 7, 2013 at 07:16 PM Muito obrigado. Já funciona 😉 Só mais uma pergunta. achas que é exequível fazer um programa em java tipo em swing onde eu crie os objectos e ele passe essa informação para xml? Cumprimentos http://www.speedtest.net/result/2682095343.png
KTachyon Posted August 7, 2013 at 08:05 PM Report #521146 Posted August 7, 2013 at 08:05 PM Eu diria que sim, mas pode depender muito da complexidade do problema. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
ricardoneves93 Posted August 7, 2013 at 11:15 PM Author Report #521167 Posted August 7, 2013 at 11:15 PM É apenas um ficheiro xml para desenhar umas plataformas, que têm tamanho (x,y), posição(x,y), cor, rotação, e mais uns parâmetros que ainda não pensei em pormenores. Mas acho que não é nada de complexo. Depois tem vários tipos de plataformas. Qual seria a melhor forma de fazer isto? http://www.speedtest.net/result/2682095343.png
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now