Pages

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, November 8, 2010

using keys in your games

images

when you want use keys  you should noticed that have many function

like keyPressed(int keyCode),keyReleased(int keyCode) ,keyRepeated(int keyCode) if you want use all of them should have something for control number of key pressing and releasing and repeating. with this code detect which key is pressed

int gameAction = getGameAction(keyCode);
switch(gameAction){
case UP:
key= "[UP]";
break;
case DOWN:
key= "[DOWN]";
break;
case LEFT:
key= "[LEFT]";
break;
case RIGHT:
key= "[RIGHT]";
break;
case FIRE:
key= "[FIRE]";
break;
case GAME_A:
message = "[LEFT_UP]";
break;
case GAME_B:
message = "[RIGHT_UP]";
break;
case GAME_C:
key= "[LEFT_DOWN]";
break;
case GAME_D:
key= "[RIGHT_DOWN]";
break;
default:
key= "";
break;

you can add all of code in same function.then define value like speed; we assume that in our game when you continue on  key press you have more speed so add this code after detect key “speed++”;But after checking speed in real device you see that you object move slower than emulator.it  because that device can’t process graphic like computer and emulator.You have to use timer functions and class in your games or using smaller graphic file in next post you see how do that


Tuesday, November 2, 2010

developing java touch games

 

iphone-finger-game

In game canvas you can detect keys and touch and using graphic function.Canvas have pre defined functions .For using touch in your game you have three function

protected  void pointerDragged(int x, int y) 
protected  void pointerPressed(int x, int y)
protected  void pointerReleased(int x, int y)

they are active when you dragged or pressed or released your finger(or pen) on your device.X and Y is place of your finger or pen.If you want same action for all of function you can define another function like this

public void doyouraction(int x,int y)
{

//add your code here

// you can change your function name(doyouraction)

}

in other hand you should have deferent code or functions

{} are keyword for define start and end of your codes.Public means that you can use your function in other class ,if you don’t want use private instead.After coding function use it with this codes

protected  void pointerDragged(int x, int y) 

{

doyouraction(int x,int y) ;
}

just this,in next post you’ll see infoo about detect keys and code for them

Thursday, October 28, 2010

about canvas class and using it in mobile game

If you want using keys or touch or graphics in your game you should use canvas class.Canvas class have predefined function for detect key press or drag or screen  touch and also important paint function .First you should add new canvas class with right click on your project and go new and select MIDP canvas…..

In this class you’ll see some function like this

protected  void keyPressed(int keyCode) {
   }
  
   /**
    * Called when a key is released.
    */
   protected  void keyReleased(int keyCode) {
   }

   /**
    * Called when a key is repeated (held down).
    */
   protected  void keyRepeated(int keyCode) {
   }

……………..

……………

…………….

……………

they are contains code that running when key pressed or released or repeated or…you can add code like draw string in keypress and create text when key pressed .Now you should running canvas in your game .For this purpose you should add code like this in startApp function in your middlet.

 Display.getDisplay(this).setCurrent(new MIDPCanvas());

jus thisWinking smile

in next post I’ll describe how you can detect fire,back,numbers,..keys and pointer into your software 

Friday, October 22, 2010

First view of java mobile app code and change it

When you build a java mobile game in software(like netbeans IDE or eclipse..)

first thing you‘ll see is  a middlet class .Middlet class used for handling start and

initialize and interface of java mobile applications. In netbeans you have sample

screen that can add button or text or image or like that .just you should drag your desired items from right side to screen and then go to source

 

netbeansscreen

screen is interface of your project in netbeans

For example you  have pre defined string seeing in screen and want changed it when middlet started. Code is like this

public void startMIDlet() {
       // write pre-action user code here
       switchDisplayable(null, getForm());
             stringItem.setText("my name is dawood");
       // write post-action user code here
   }

startMIDlet is function that using in every middlet .In netbeans for adding interface to screen using a form , should calling when middlet started.For this purpose have this code switchDisplayable(null, getForm());

and then add this for changing text stringItem.setText("my name is dawood");

and run it in emulator

emulator1

in start for do more thing in java games you should add canvas class that i’ll describe later

Thursday, October 7, 2010

start coding for your game

now we can start to coding for game.you can modeling your world

when coding your game  but it confuse you. It is better  that first

modeling and then coding and if it isn’t satisfying restart your process.It

is called new version!can be called 0.0.1 or things like that .In version

number commonly  first digit describe mane change in your game and

second describe partial change and third contains number of build(run)

of your game(on base of company strategy) .Now you should design

algorithm of your game before start to  any codes.Algorithm is your

game or software description in sample language(not in codes).For

example for add two digits algorithm is

1.get first number and save it

2.get second number and save it too

3.add two number

4.save result and print

see Algorithm definition and everything here

in next  posts i will  describe a sample algorithm and codes of

games .For now is sufficient …one minute somebody miss algorithm  and

going to coding without   it,It is true just about too sample game ,In

other games You spend many time and maybe see a failed project!

linkwithin

Related Posts Plugin for WordPress, Blogger...