[EXAMPLE] Tiled game *UPDATED*

  ... tutorials on how to use AndEngine.

[EXAMPLE] Tiled game *UPDATED*

Postby rioneye » Mon Aug 01, 2011 9:18 pm

Hey Everyone,

This project contains everything I've been experimenting with over the last three weeks. I thought I would share it, since some of you may face similar problems down the road. I should note that I am pretty new to andEngine and programming in general so don't expect anything too fancy. Also some of the code may not be obvious at first, so if you need an explanation of anything let me know.

This project covers:
-SplashScreen, Options, and Menus
-Basic A* pathing
-TMXMap switching w/AsyncTask(including multiple entrances and exits)
-Tile Animation
-Tile Properties (Events triggered under certain conditions)

Here is what you'll see when you run the project:
device-2011-08-01-124047.png
device-2011-08-01-124047.png (442.29 KiB) Viewed 20014 times


At
1) There is the layer where the player can pass under(really simple to do)
2) There are the animating tiles
3) There is the exit to the large Map (it was large for testing purposes)
4) There is my shack that you can enter :) (The floor looks strange when you walk across though)
5) There is the second entrance to the large map
Note: The large map takes anywhere from 10 to 30 seconds to load depending on your device. If you use the emulator or are connected to the debugger then it may take magnitudes longer. However, the map is stored in memory so you only need to load it once then it should take less then a couple seconds to load if you go back in

I should also note that the Dpad still needs works.

EDIT: I forgot to mention that the grey tiles at the top change when the player steps on them.

**************************************************UPDATE*******************************************************
So after many delays I have finally updated this project to GLES 2 and have updated the code such that it is more clean and easier to understand (at least I hope it is :) ). Most of the functionality from the GLES 1 build is still there, but there are a couple of additions.

This project includes:
-SplashScreen, Options, and Menus
-Scene switching
-Sequences
-Basic A* pathing
-TMXMap switching w/AsyncTask(including multiple entrances and exits)
-Tile Animation
-Tile Properties (Events triggered under certain conditions)

I am not completely happy with this build but I figured that something is better than nothing and this should help the people who have been struggling to get started with GLES2. So here is what you will see when you press 'play' on the menu:

device-2012-12-18-175735.png
device-2012-12-18-175735.png (32.86 KiB) Viewed 7361 times


At
1) There is the layer where the player can pass under(really simple to do)
2) There are the animating tiles
3) There is the exit to the second Map (it was large for testing purposes)
4) There is the second entrance to the second map
5) There is a layer that changes tiles when you walk across it

*Just like last time the DPad still needs some work. Its a little jittery.

If you find this useful then please go to my new project page and help test and support my new project. I've also started a kickstarter for the project which has just gone live (as of 12/18/12) and could use some support as well...if you know what I mean :D .
Project page: http://www.andengine.org/forums/project-development-blogs/evocreo-t10232.html
Kickstarter: http://kck.st/U7st4i

EDIT: I forgot, I made edits to andEngine so you will need to download the andengine project and the TMXTiled Extension I used as well as the tiled game example. If you choose not to then, you will need to edit the engine yourself whenever you get errors.
AndEngine.zip
Modified andengine project. To be used with TiledGameExample
(2.49 MiB) Downloaded 797 times

AndEngineTMXTiledMapExtension.zip
Modified TMX extension. To be used with TiledGameExample
(145.75 KiB) Downloaded 775 times
Attachments
TiledGameExample.zip
This is the newest project for GLES2
(6.22 MiB) Downloaded 1198 times
Copy of GameTestAndEngine.zip
This is the old project for GLES1
(7.06 MiB) Downloaded 8169 times
Last edited by rioneye on Wed Jan 09, 2013 5:35 am, edited 9 times in total.
User avatar
rioneye
 
Posts: 277
Joined: Tue Jun 28, 2011 7:08 pm
Location: USA

Re: [Tutorial] Tiled game

Postby Mathew » Tue Aug 02, 2011 2:27 am

Really good work, thanks for sharing, will be useful for sure for me now.

Edit, while testing on my HTC Wildfire S, I noticed some problems (stopped unexpectedly)

When you will try to touch walls on the bottom (ends of the map) and right side of the map. (NPE`s)

Also you are using only one library (andengine.jar) so rest are useless, thats why app is so big (6mb)
User avatar
Mathew
 
Posts: 1073
Joined: Sun Jul 31, 2011 2:49 pm
Location: Tarnów, Poland

Re: [Tutorial] Tiled game

Postby rioneye » Tue Aug 02, 2011 6:57 pm

Mathew wrote:Really good work, thanks for sharing, will be useful for sure for me now.

Edit, while testing on my HTC Wildfire S, I noticed some problems (stopped unexpectedly)

When you will try to touch walls on the bottom (ends of the map) and right side of the map. (NPE`s)

Also you are using only one library (andengine.jar) so rest are useless, thats why app is so big (6mb)



Thanks for the advice, I took out the jar's but it seems to have increased the size :). Also I found where the errors were (assuming you were using the Dpad) and fixed them in the new project. Although you could fix it yourself since its not that large of a change. If you go to line 53 in the UI class the block of code should look like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //sets the Dpad limits to within the Tiled map
  2. if (moveToXTile < TMXMapLayer.getWidth() && moveToYTile < TMXMapLayer.getHeight()
  3.                 && moveToXTile >= 0 && moveToYTile >= 0) {
  4.         //Sets the path to take from the values obtained by DPad
  5.         walkTo(moveToXTile, moveToYTile, mScene);
  6. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

The equality signs have been adjusted.

Thanks Again
User avatar
rioneye
 
Posts: 277
Joined: Tue Jun 28, 2011 7:08 pm
Location: USA

Re: [Tutorial] Tiled game

Postby kblood » Wed Aug 03, 2011 3:50 pm

This is great :) My fix to the digital controls was to put in the same check there is when you click, that makes sure it clicks within the map or on a tile. Same thing works with the digital control.

Seems you have made the digital control work much better. Minimal stuttering now :) Not even sure there is any actual stuttering. Great job. I cannot think of anything to add at the moment. Impressive with the animated tiles, that gives some interesting options, like animated trees and such. With trees it might be an idea to let the bottom half be collidable when the tree is 4 tiles big, and the top being textures you walk behind. But I guess your current tile set does not have any of those big trees.

I will through it all some more, and try to understand the code. Sure is many things in it.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Tiled game

Postby kblood » Wed Aug 03, 2011 4:44 pm

I tested it on my HTC Hero, and the framerate look bad. I had to make the minSDK lower so it would support 2.1.

It also crashes on the hero in the big rooms. I guess it cannot handle such big rooms.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Tiled game

Postby rioneye » Wed Aug 03, 2011 5:25 pm

kblood wrote:I tested it on my HTC Hero, and the framerate look bad. I had to make the minSDK lower so it would support 2.1.

It also crashes on the hero in the big rooms. I guess it cannot handle such big rooms.


You mean it crashes on the 200x200 tile map or the shack? I tested it on an LG optimus and it seemed to work fine. That is strange that it crashes on the Hero, since I thought the Optimus was similar to the Hero. Still if you could use debugger to see what the error that was would greatly help. Also to improve the frame rate make sure you're disconnected from the debugger
Debug.png
Debug.png (20.15 KiB) Viewed 19878 times
User avatar
rioneye
 
Posts: 277
Joined: Tue Jun 28, 2011 7:08 pm
Location: USA

Re: [Tutorial] Tiled game

Postby kblood » Thu Aug 04, 2011 10:43 am

I will try. The Hero is a phone with some 300mb ram only, and most of them are reserved for Android core systems. And it is less than 500mhz. I think it just cannot handle this amount of layers and graphics. And it is also only running Android 2.1

I will try to see what the error message is when I get home.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Tiled game

Postby kblood » Thu Aug 04, 2011 7:00 pm

I believe this is the relevant part of the error:

08-04 18:56:28.636: ERROR/HTCIMEService(161): [onFinishInput] mData.mCurrIM.finishInput(); Exception = java.lang.NullPointerException
08-04 18:56:31.456: ERROR/HTCIMEService(161): [onFinishInput] mData.mCurrIM.finishInput(); Exception = java.lang.NullPointerException
08-04 18:57:10.986: ERROR/AndroidRuntime(8103): Uncaught handler: thread UpdateThread exiting due to uncaught exception
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): java.lang.IndexOutOfBoundsException: Invalid location 6, size is 0
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at java.util.ArrayList.get(ArrayList.java:341)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.entity.Entity.onManagedUpdate(Entity.java:1021)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:247)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.entity.Entity.onUpdate(Entity.java:879)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.engine.Engine.onUpdateScene(Engine.java:485)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.engine.Engine.onUpdate(Engine.java:480)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.engine.Engine.onTickUpdate(Engine.java:456)
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at org.anddev.andengine.engine.Engine$UpdateThread.run(Engine.java:682)
08-04 18:57:11.056: ERROR/dalvikvm(8103): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Especially these lines:
08-04 18:56:31.456: ERROR/HTCIMEService(161): [onFinishInput] mData.mCurrIM.finishInput(); Exception = java.lang.NullPointerException
08-04 18:57:10.986: ERROR/AndroidRuntime(8103): Uncaught handler: thread UpdateThread exiting due to uncaught exception
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): java.lang.IndexOutOfBoundsException: Invalid location 6, size is 0
08-04 18:57:10.996: ERROR/AndroidRuntime(8103): at java.util.ArrayList.get(ArrayList.java:341)

Oh, and it does not happen on my Samsung Galaxy S, so I do think it is just because of the lack of ram or some other kind of memory.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: [Tutorial] Tiled game

Postby rioneye » Thu Aug 04, 2011 8:07 pm

Well if it is ram, try reducing the size of the large map from 200x200 tiles to something like 100x100. I was testing out loading times when I made it that large. Not sure why the null pointer or the out of bound index would show up, though. It should stay the same regardless of the device that is using it. Maybe someone with more experience working with multiple devices could explain it.
User avatar
rioneye
 
Posts: 277
Joined: Tue Jun 28, 2011 7:08 pm
Location: USA

Re: [Tutorial] Tiled game

Postby kblood » Fri Aug 05, 2011 10:12 pm

I think it is just because some of the maps are larger than intended. But I doubt it will matter unless you expect to release it for older phone models. The HTC Hero is second or third generation Android mobile.

I have been playing around with your old map, because I had some problems remaking the new one. I was testing out something I have thought about trying for a while now.
http://dionysus.dk/devstuff/downloads/apathgame1.zip

I made it possible to change tiles in-game. Since your collision detection was to check for specific tile-ids, instead of using an array, it has no problem seeing how a tile got changed into one that is not collide able.

If you click the button, you cannot click move the character, but every tile you click will become a desert tile, which can be walked on. I made the map have some extra collide able tiles to test it with. Its a modification of the apath4 script, where I added pinch zooming and drag scrolling.

I will try to see if I can do the same thing in your new example, but I will probably have to find a way to take it a bit apart first to make it run on my HTC Hero. That is usually my test phone.

The most relevant code is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent){
  3.  
  4. // I removed some code here for pinchzooming and such, which is not relevant here.
  5.  
  6.                 if(pSceneTouchEvent.isActionUp()) {  //this checks for when a screen touch is released
  7.                        
  8. // I have a button added to the HUD, and it switched a "godlike" boolean between true and false. When true, it changes the tiles you touch after enabling the button, into desert tiles)
  9.                         if (godlike == true) {
  10.  
  11.                                 //button1.setColor(0.0f, 1.0f, 0.0f);  // I wanted to change the color of the button to see the status of the godmode, but seems it is only possible to change this before everything is loaded.
  12.  
  13.                     /* Get the tile where the center of the current tile is. */
  14.                     tmxTile = tmxLayer.getTMXTileAt(pSceneTouchEvent.getX(),
  15.                                 pSceneTouchEvent.getY()); // checks what tile you touched
  16.                     if(tmxTile != null) { // makes sure there is a tile here
  17.                         //tmxTile.setTextureRegion(null); //<-- Rubber-style removing of tiles =D
  18.  
  19.                         tmxTile.setGlobalTileID(mTMXTiledMap, 30); // tileid 30 is the desert tile
  20.  
  21.                     }
  22.                                
  23.                         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


This should mean it is very possible to use this code to make a Tower Defense map, a boulderdash game, a 2D dungeon keeper and quite a few other things.

But I think I will have to take your code apart and make my own to make sure I know what everything actually does. I need to figure out ways to load several layers, and look into why maps often crashes when using another TMX.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Next

Return to Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 38 guests