|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Mar 2008
Messaggi: 401
|
[J2ME]Errore... ma dov'è???
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: Codice:
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;
}
}
Codice:
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) {}
}
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? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Mar 2008
Messaggi: 401
|
Ehi, c'è nessuno?!?
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
perchè ovviamente devi racchiudere il blocco di codice che lancia una IOException in un blocco try catch.....
ma hai mai programmato in java o sei partito sparato con j2me?
__________________
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Mar 2008
Messaggi: 401
|
Ho fatto così:
Codice:
public MioCanvas()
{
super(true);
Graphics g2 = getGraphics();
try{
img = Image.createImage("/pallaTraspAni.png");
sprite = new Sprite(img, 32, 32);
muro = Image.createImage("/muro.png");
mappaImg = new Sprite[righe][colonne];
//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);
mappaImg[i][j].paint(g2);
}
}
}
}catch (IOException ex)
{
System.err.println("Errore: " + ex);
}
posX = 10;
posY = 10;
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 23:49.



















