Before we can create the sprites we need to load the images for them. The code is straightforward:
Image backgroundImage; Image cloudImages[]; Image playerShipImage; Image playerBulletImage; Image playerMissileImage; Image enemyImage; ... private void loadResources() { // background // -------------------------------- backgroundImage = new Image( getClass().getResource( "assets/maps/canyon.jpg").toExternalForm()); // player ship // -------------------------------- playerShipImage = new Image( getClass().getResource( "assets/vehicles/anansi.png").toExternalForm()); // clouds // -------------------------------- cloudImages = new Image[4]; cloudImages[0] = new Image( getClass().getResource( "assets/environment/cloud-01.png").toExternalForm()); cloudImages[1] = new Image( getClass().getResource( "assets/environment/cloud-02.png").toExternalForm()); cloudImages[2] = new Image( getClass().getResource( "assets/environment/cloud-03.png").toExternalForm()); cloudImages[3] = new Image( getClass().getResource( "assets/environment/cloud-04.png").toExternalForm()); // bullets playerBulletImage = new Image( getClass().getResource( "assets/bullets/bullet_01.png").toExternalForm()); // enemy ship: tormentor enemyImage = new Image( getClass().getResource( "assets/vehicles/tormentor.png").toExternalForm()); }
There isn't much to add to this, it's simple. Since everything is very similar one could create an identifier (eg enum) for each image type and put it into a map with the filename as value, iterate the map and load the resources that way. But we'll leave that for later when we create multiple levels and need a resource manager.
As you may already suggest, this method would also load other resources like e. g. sound files.
No comments:
Post a Comment