agente mm8
08-07-2008, 16:17
Ho iniziato a giocherellare con j2me, e sto iniziando a creare qualche gioco aiutandomi con deio tutorial.
Il mio problema è questo: ho creato la classe MioCanvas:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.IOException;
public class MioCanvas extends GameCanvas implements Runnable
{
private Image img;
private Sprite sprite;
private boolean inGioco;
private int posX, posY;
//mappa
private Sprite[][] mappaImg;
private int righe,colonne;
private Image muro;
private int[][] mappa = {
{0,0,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,1,0,0,0,1},
{1,0,0,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
public MioCanvas()
{
super(true);
try{
img = Image.createImage("/pallaTraspAni.png");
sprite = new Sprite(img, 32, 32);
}catch (IOException ex)
{
System.err.println("Errore: " + ex);
}
posX = 10;
posY = 10;
//mappa
mappaImg = new Sprite[righe][colonne];
muro = createImage("/muro.png");
//riempimento tabella sprite
for(int i = 0; i < righe; i++)
{
for(int j = 0; j < colonne; j++)
{
if(mappa[i][j] == 1)
{
mappaImg[i][j] = new Sprite(muro, 16, 16);
mappaImg[i][j].setPosition(posX+16*j, posY+16*i);
}
}
}
}
private void input()
{
//non ho inserito il controllo della calpestabilità, ma fate come che ci sia
int tasto = getKeyStates();
if((tasto & LEFT_PRESSED) != 0){
sprite.setFrame(2);
posX -= 5;
}
if((tasto & RIGHT_PRESSED) != 0){
sprite.setFrame(1);
posX += 5;
}
if((tasto & UP_PRESSED) != 0){
sprite.setFrame(3);
posY -= 5;
}
if((tasto & DOWN_PRESSED) != 0){
sprite.setFrame(0);
posY += 5;
}
}
public void disegna()
{
Graphics g = getGraphics();
g.setColor(187, 213, 251);
g.fillRect(0, 0, getWidth(), getHeight());
sprite.setPosition(posX, posY);
sprite.paint(g);
//mappa
for(int i = 0; i<righe; i++)
{
for(int j = 0; j<colonne; j++)
{
//se c'è qualcosa da disegnare disegnalo
if(mappa[i][j] == 1)
{
mappaImg[i][j].paint(g);
}
}
}
flushGraphics();
}
public void start()
{
inGioco = true;
Thread t = new Thread(this);
t.start();
}
public void run()
{
while(inGioco == true)
{
input();
disegna();
try{Thread.sleep(50);}
catch(InterruptedException ie) {}
}
}
public void stop()
{
inGioco = false;
}
}
e la classe MioMidlet:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MioMidlet extends MIDlet
{
private Display d;
private MioCanvas schermata;
public MioMidlet()
{
d = Display.getDisplay(this);
schermata = new MioCanvas();
schermata.start();
}
protected void startApp()
{
d.setCurrent(schermata);
}
protected void pauseApp() {}
protected void destroyApp(boolean unconditional) {}
}
Faccio build sul Toolkit e indovinate un po'?
Seleziona la riga "muro = Image.createImage("/muro.png");" e dice:
"unreported exception java.io.IOException; must be caught or declared to be thrown".
Perche mi dà questo errore?
Che faccio?
Il mio problema è questo: ho creato la classe MioCanvas:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.IOException;
public class MioCanvas extends GameCanvas implements Runnable
{
private Image img;
private Sprite sprite;
private boolean inGioco;
private int posX, posY;
//mappa
private Sprite[][] mappaImg;
private int righe,colonne;
private Image muro;
private int[][] mappa = {
{0,0,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,1,0,0,0,1},
{1,0,0,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
public MioCanvas()
{
super(true);
try{
img = Image.createImage("/pallaTraspAni.png");
sprite = new Sprite(img, 32, 32);
}catch (IOException ex)
{
System.err.println("Errore: " + ex);
}
posX = 10;
posY = 10;
//mappa
mappaImg = new Sprite[righe][colonne];
muro = createImage("/muro.png");
//riempimento tabella sprite
for(int i = 0; i < righe; i++)
{
for(int j = 0; j < colonne; j++)
{
if(mappa[i][j] == 1)
{
mappaImg[i][j] = new Sprite(muro, 16, 16);
mappaImg[i][j].setPosition(posX+16*j, posY+16*i);
}
}
}
}
private void input()
{
//non ho inserito il controllo della calpestabilità, ma fate come che ci sia
int tasto = getKeyStates();
if((tasto & LEFT_PRESSED) != 0){
sprite.setFrame(2);
posX -= 5;
}
if((tasto & RIGHT_PRESSED) != 0){
sprite.setFrame(1);
posX += 5;
}
if((tasto & UP_PRESSED) != 0){
sprite.setFrame(3);
posY -= 5;
}
if((tasto & DOWN_PRESSED) != 0){
sprite.setFrame(0);
posY += 5;
}
}
public void disegna()
{
Graphics g = getGraphics();
g.setColor(187, 213, 251);
g.fillRect(0, 0, getWidth(), getHeight());
sprite.setPosition(posX, posY);
sprite.paint(g);
//mappa
for(int i = 0; i<righe; i++)
{
for(int j = 0; j<colonne; j++)
{
//se c'è qualcosa da disegnare disegnalo
if(mappa[i][j] == 1)
{
mappaImg[i][j].paint(g);
}
}
}
flushGraphics();
}
public void start()
{
inGioco = true;
Thread t = new Thread(this);
t.start();
}
public void run()
{
while(inGioco == true)
{
input();
disegna();
try{Thread.sleep(50);}
catch(InterruptedException ie) {}
}
}
public void stop()
{
inGioco = false;
}
}
e la classe MioMidlet:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MioMidlet extends MIDlet
{
private Display d;
private MioCanvas schermata;
public MioMidlet()
{
d = Display.getDisplay(this);
schermata = new MioCanvas();
schermata.start();
}
protected void startApp()
{
d.setCurrent(schermata);
}
protected void pauseApp() {}
protected void destroyApp(boolean unconditional) {}
}
Faccio build sul Toolkit e indovinate un po'?
Seleziona la riga "muro = Image.createImage("/muro.png");" e dice:
"unreported exception java.io.IOException; must be caught or declared to be thrown".
Perche mi dà questo errore?
Che faccio?