public void configProperties(GameObject curObj, String property, String data_Str) { switch (Properties.toProperty(property)) { case x: Float x = new Float(data_Str); curObj.setX(x); break; case y: Float y = new Float(data_Str); curObj.setY(y); break; case dx: Float vel_X = new Float(data_Str); curObj.setVelocityX(vel_X); break; case dy: Float vel_Y = new Float(data_Str); curObj.setVelocityY(vel_Y); break; case total: Integer total = new Integer(data_Str); curObj.setTotal(total); break; case stgy: Integer stgy = new Integer(data_Str); curObj.setStgy(stgy); break; case Unknown: System.err.println("Found unknown property: " + property); break; } }
// private otherwise not safe - since constructor calling this method private void loadObjects() { staticGameObjs = new ArrayList<GameObject>(); animatedGameObjs = new ArrayList<GameObject>(); InputStream in = null; try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); in = en.loadData(configFile); // this.setStream(in); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); // Sprite sprite = null; GameObject curObj = null; // level = null; while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); // If we have a item element we create a new item if (startElement.getName().getLocalPart().equals(OBJ_SPRITE)) { Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { Attribute attribute = attributes.next(); if (attribute.getName().toString().equals(SPRITE_TYPE)) { String spriteName = attribute.getValue(); curObj = createGameObjects(spriteName); } if (attribute.getName().toString().equals(NATURE)) { curObj.setNature(attribute.getValue()); } } } else if (event.isStartElement() && curObj != null) { String property = event.asStartElement().getName().getLocalPart(); event = eventReader.nextEvent(); String data_Str = event.asCharacters().getData(); configProperties(curObj, property, data_Str); } } if (event.isEndElement() && curObj != null) { EndElement endElement = event.asEndElement(); if (endElement.getName().getLocalPart().equals(OBJ_SPRITE)) { if (curObj.getIsAnimated()) { this.animatedGameObjs.add(curObj); // Create remaining GameObjects defined in xml tag <total> if (curObj.getTotal() > 1) { for (int i = 0; i < curObj.getTotal() - 1; i++) { GameObject newCurObj = createGameObjects(curObj.getName()); newCurObj.setX((float) Math.random() * 600); newCurObj.setY((float) Math.random() * 100); newCurObj.setName(curObj.getName()); newCurObj.setStgy(curObj.getStgy()); this.animatedGameObjs.add(newCurObj); } } } else this.staticGameObjs.add(curObj); } } } // Send GameObject arrays to current Level this.level.setArrGameAnim(animatedGameObjs); this.level.setArrGameStatic(staticGameObjs); } catch (FileNotFoundException ex) { } catch (XMLStreamException e) { } }