Dans le cadre d'un projet pour la fac, je suis en train de réaliser un petit jeu. Celui-ci consiste à bouger un personnage avec le clavier afin qu'il attrape ou évite des objets qui lui tombe dessus. Pour faire "tomber" les objets j'utilise des threads qui sont eux-même utilisés dans un thread qui cré de nouveaux objets régulièrement. Jusque là tout va bien!
A la compilation, aucun problème, mais au lancement, les objets tombent bien régulièrement mais je n'arrive pas à bouger mon personnage avec le clavier dans un premier temps. Il faut que j'aille sur une autre fenetre et que je revienne sur celle de mon jeu pour que le personnage réagisse finalement au clavier...quel est le problème? Je vous laisse mon code(un peu brouillon, )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class MainPane implements KeyListener{
/...je ne poste pas le code qui definit le paneau principal(un peu trop long), et ajoute un bouton start qui lance cominciaPartita().../
public synchronized void cominciaPartita(){
		changeBandiere();
		Runnable r = new PartitaRunnable(this, mainPane,datiGiocatori.get(0), datiGiocatorePanels.get(0));
		Thread t = new Thread(r);
		t.start();
	}
	public void addMoneta(){
		Moneta moneta = new Moneta();
		int i =0;
		for (WorldPanel wp : worldPanels){
			wp.addMoneta(moneta);
			Runnable r = new MonetaRunnable(moneta,wp,personnagio,datiGiocatorePanels.get(i), datiGiocatori.get(i));
			Thread t = new Thread(r);
			t.start();
			i++;
		}
	}
	public void addBomba(){
		Bomba bomba = new Bomba();
		int i =0;
		for (WorldPanel wp : worldPanels){
			wp.addBomba(bomba);
 
			Runnable r = new BombaRunnable(bomba,wp,personnagio,datiGiocatorePanels.get(i), datiGiocatori.get(i));
			Thread t = new Thread(r);
			t.start();
			i++;
		}
	}
	public void changeBandiere(){
		ListeDrapeaux liste = new ListeDrapeaux();
		Runnable r = new ListeDrapeauRunnable(liste,datiGiocatori.get(0));
		Thread t = new Thread(r);
		t.start();
		for (PanelBandiere pb : panelBandieres){
			pb.add(liste);	
			Runnable r2 = new DrapeauRunnable( pb,datiGiocatori.get(0));
			Thread t2 = new Thread(r2);
			t2.start();
		}
	}
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		int codeSinistra = 37;
		int codeDestra =39;
		WorldPanel giocatorePrincipalePanel = worldPanels.get(0);
		if (code == codeSinistra) {
			personnagio.moveVersoSinistra();
			giocatorePrincipalePanel.repaint();
		} else if (code == codeDestra) {
			personnagio.moveVersoDestra();
			giocatorePrincipalePanel.repaint();
		}
	}
	public  void keyReleased(KeyEvent e){
	}
	public  void keyTyped(KeyEvent e){
	}
}
merci de vos réponses