If have login problems remove cookies and clear browser cache.



Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[DEPRECATED TUTORIAL] Your first game engine tutorial
05-20-2021, 09:31 PM (This post was last modified: 05-20-2021 09:33 PM by JoseskVolpe.)
Post: #1
Exclamation [DEPRECATED TUTORIAL] Your first game engine tutorial
That's a deprecated tutorial, wich means i'll be not supporting it. I'm just giving it here because it may be interest to someone and i want to make the J2ME SDK Mobile article shorter
Download and extract Game base.zip instead


Your first game engine tutorial (DEPRECATED, i'm keeping it on here because it can be your interest for it to stay here)


(04-30-2021 11:53 PM)star cash Wrote:  Where should i click to start creating game

First, don't forgot to set file write/read permissions to "Allowed" or "Ask one time"

Go to "New Project...", specify your Project's name and its Folder Location, and click create. This will create a new project. > Click "Create" Soft Button and Create.
Now you should create your first package. Go to Source Packages > Left/Right Soft Key (it depends on your phone) > New > Java Package > Create > Specify your package name with dots and everything lower case (for example: "game.starcash") > right/left soft and "Finish"

Open your new package > left/right shoft > New > Java MIDlet > Name it anything > then create

A code will apear:

Code:
package game.starcash;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Game extends MIDlet{

public void startApp(){
//When player opens or resume your game
}

public void pauseApp(){
//When player minimizes your game, or receives a call, or system pauses it
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
//When player closes your game
}

}

Now you need to create a displayable and invoke it, you can create a new Java file for it, but you can make it inside the same file in the same class

Add the following line:

Code:
import javax.microedition.lcdui.game.GameCanvas;
class GameEngine extends GameCanvas{
protected GameEngine(boolean suppressKeyEvents) {
super(suppressKeyEvents);
//when invoked by the Game class
}

public void paint(Graphics g){
//Use this to render your game
//You can also use this to update each frame, although it will use the same thread
repaint();
}

public void Update(){
//Use this to update each frawme
}
}

Your code will look like this:

Code:
package game.starcash;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Game extends MIDlet{

public void startApp(){
//When player opens or resume your game
}

public void pauseApp(){
//When player minimizes your game, or receives a call, or system pauses it
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
//When player closes your game
}

class GameEngine extends GameCanvas{
protected GameEngine(boolean suppressKeyEvents) {
super(suppressKeyEvents);
//when invoked by the Game class
}

public void paint(Graphics g){
//Use this to render your game
//You can also use this to update each frame, although it will use the same thread
repaint();
}

public void Update(){
//Use this to update each frame
}
}

}

Now you invoke wit into your startApp() class and begin it
Don't forget startApp() is also used when you're resuming it, so you need to store in a variable your app was already started
Code:
boolean gameStarted=false; //False for the first startApp()
public void startApp(){
if(!gameStarted){
//First start()
}
gameStarted=true;
}

And invoke your GameEngine

Code:
GameEngine engine;

public void startApp(){
//When player opens or resume your game
    if(!gameStarted){
        //First start()
        engine = new GameEngine(false);
        }
        gameStarted=true;
        
        
        Display.getDisplay(this).setCurrent(engine);
}

Your code will look like this:

Code:
package game.starcash;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;

public class Game extends MIDlet{

boolean gameStarted=false; //False for the first startApp()
GameEngine engine;
    
public void startApp(){
//When player opens or resume your game
    if(!gameStarted){
        //First start()
        engine = new GameEngine(false);
        }
        gameStarted=true;
        
        
        Display.getDisplay(this).setCurrent(engine);
}

public void pauseApp(){
//When player minimizes your game, or receives a call, or system pauses it
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
//When player closes your game
}

class GameEngine extends GameCanvas{
protected GameEngine(boolean suppressKeyEvents) {
    super(suppressKeyEvents);
//when invoked by the Game class
}

public void paint(Graphics g){
//Use this to render your game
//You can also use this to update each frame, although it will use the same thread
repaint();
}

public void Update(){
//Use this to update each frawme
}
}

}

You are almost there, now you need to create a new thread, or in a easier way for this example, create a loop for the Update() method into the GameEngine, running separated from the paint() Thread:

Code:
while(true) {
            engine.Update();
        }

You can also use Thread.sleep() for limiting the FPS, but Java phones are very slow in performance so you won't need it at all, only for emulators, wich has FPS Limit in their settings

Now this is your first Game Engine code:W

Code:
package game.starcash;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;

public class Game extends MIDlet{

boolean gameStarted=false; //False for the first startApp()
GameEngine engine;
    
public void startApp(){
//When player opens or resume your game
    if(!gameStarted){
        //First start()
        engine = new GameEngine(false);
        }
        gameStarted=true;
        
        
        Display.getDisplay(this).setCurrent(engine);
        
        while(true) {
            engine.Update();
        }
}

public void pauseApp(){
//When player minimizes your game, or receives a call, or system pauses it
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
//When player closes your game
}

class GameEngine extends GameCanvas{
protected GameEngine(boolean suppressKeyEvents) {
    super(suppressKeyEvents);
//when invoked by the Game class
}

public void paint(Graphics g){
//Use this to render your game
//You can also use this to update each frame, although it will use the same thread
repaint();
}

public void Update(){
//Use this to update each frawme
}
}

}

Follow the comment instructions, and good work :3

You will see there'll be nothing dispaying on your game, or it'll be stuck in the loading time. It's because there's nothing for it to paint on the screen. Play with the g variable (Graphics class) on the paint() method and have fun

[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
[DEPRECATED TUTORIAL] Your first game engine tutorial - JoseskVolpe - 05-20-2021 09:31 PM

Forum Jump:


User(s) browsing this thread: 2 Guest(s)