开发者

Triggering a method from one class to an other using a button in Android SDK

开发者 https://www.devze.com 2023-04-03 06:38 出处:网络
I\'m sorry if this is asked too much but I couldn\'t find something clear enough. I\'m developing a little game engine for android.

I'm sorry if this is asked too much but I couldn't find something clear enough.

I'm developing a little game engine for android. I've already implemented a gamepad cl开发者_Python百科ass to control the touch screen. The game pad class draws an analog stick if someone touches the right area and also draws to buttons on the side.

The problem is , making it simple, i have the main class GAME that as the LEVEL class and GAMEPAD class.

Inside the GAMEPAD code i can trigger things when someone pressed\releases a button. The problem is I wan't to trigger something to the main GAME class by pushing a button witch is handled in the GAMEPAD class.

In C# i would use events and delegated.

In Java i have no clue.. i want the gpad to invoke a method to who is registered.

This is what i'm hoping to achieve on the main GAME class.

.. void onGpadClick (e EventArgument) { if (e.Button==Button.A&&e.ButtonState==ButtonState.Pressed) Shoot();

}

Thanks in advance


You can achieve this using Intents.

http://www.vogella.de/articles/AndroidIntent/article.html


What you could do is this:

  1. Let your GAMEPAD-class recieve an instance GAME-class (either in its constructor or by a set-method)
  2. In your GAME-class, you add methods containing what ever you want done for each button e.g. onButtonAClicked(), onButtonBClicked() and onPadClicked(). (If those are the options - add more if you have more options).
  3. Detect the clicks from your GAMEPAD-class by setting a OnClickListener on each button e.g. buttonA.setOnClicklistner( new OnClickListener() { //override the onClick-method here } ) Then override the onClick-method and add your implementation there e.g. myGame.onButtonAClicked().

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号