Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Resources from drawable folder - AndEngine

I am working on a game and I want to set the layout of game such that it works on Multiple device screens. So, instead of fetching images from asset's folder, I making looking for some way to fetch it from the drawable folder. So, that later on I can get the images according to the screen of the device.

Update:

I tried it using

mFaceTextureRegionLifeLine = (TiledTextureRegion) TextureRegionFactory.createFromResource(mTextureLifeLine, this, R.drawable.icon, 100, 100);

And, it fired me with and error below-

09-05 19:02:38.923: ERROR/AndroidRuntime(4161): FATAL EXCEPTION: main
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.engine/com.engine.BallDemo}: java.lang.ClassCastException: org.anddev.andengine.opengl.texture.region.TextureRegion
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.os.Looper.loop(Looper.java:123)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at java.lang.reflect.Method.invoke(Method.java:521)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at dalvik.system.NativeStart.main(Native Method)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): Caused by: java.lang.ClassCastException: org.anddev.andengine.opengl.texture.region.TextureRegion
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at com.engine.BallDemo.onLoadResources(BallDemo.java:132)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at org.anddev.andengine.ui.activity.BaseGameActivity.onCreate(BaseGameActivity.java:57)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161):     ... 11 more

So, if it is possible let me know. Thanks, Suri Sahani.

like image 822
Lalit Poptani Avatar asked Sep 05 '11 13:09

Lalit Poptani


2 Answers

Here's the method that will do this for you:

public static TextureRegion createFromResource(final Texture pTexture, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY)

If you need to create a tiled texture region, use this:

public static TiledTextureRegion createTiledFromResource(final Texture pTexture, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY, final int pTileColumns, final int pTileRows)

Hope this helps.

like image 123
Egor Avatar answered Nov 22 '22 15:11

Egor


If you simply specify the drawble id (Such as R.drawable.icon) then depending on your device it will pull it from the folder in /res/ which is most appropriate to the device:

ie,

/res/drawable/icon.png 
/res/drawable-large/icon.png

Would mean large devices used the second png.

See the android site for more information on supporting multiple screens.

like image 29
Graeme Avatar answered Nov 22 '22 16:11

Graeme