[J2ME] Sprite veranderen bij collision

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Kwartjuh
  • Registratie: Mei 2002
  • Laatst online: 25-08 12:54
Hallo!

Ik probeer een soort 'fly the copter' game te maken met de J2ME omgeving.
Ik maak gebruik van een TiledLayer voor om het level op te bouwen. Het helicoptertje zakt naar beneden als de gebruiker niets doet. Word de 0-toets ingedrukt, dan gaat het helicoptertje weer naar boven.

De animatie van de helicopter bestaat uit 2 Frames. Er is nog een derde Frame van de animatie, dit is de 'kapotte' status, dus als de helicopter de TiledLayers aanraakt moet de animatie stoppen en de 3e frame laten zien.

Ik ben niet echt een expert op het gebied van programmeren, en ben de kluts kwijt. Kan iemand me een eindje op weg helpen om de helicopter de animatie te laten stoppen en de 3e frame te laten zien?

code:
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.io.*;
    
    public class MyCanvas extends Canvas{
        
        private int y=0;
        private int xViewPort;
        private int loops;
        private Image helicopter;
        private Sprite copter;
        private boolean directionDown = true;
        boolean copterDead = false;
        
        Image backGroundTiles;
        
        private LayerManager layerManager;
        private TiledLayer backGround;
        private int[] animation = {0,1};

        public MyCanvas() { // constructor
        maakAchterGrond(); // methode to initialise the backGround

        layerManager = new LayerManager();
        
        layerManager.append(backGround);
        
        
        try{
                helicopter =  Image.createImage("/helicopter-rc5.png");
            }
            catch (IOException e) {  System.out.println(e); }
            
            copter = new Sprite(helicopter,50,35);
            copter.defineReferencePixel(20,12);
            copter.setFrameSequence(animation);
            layerManager.append(copter);
        }

        public void paint(Graphics g) { // draw to the screen
        
                              
        g.setColor(55,55,55);
        g.fillRect( 0, 0, getWidth(), getHeight());
        layerManager.setViewWindow(xViewPort, 0, 177, 180); 
        if (loops == 2){        
            xViewPort ++;
            loops = 0;
        }
        loops++;
        
        layerManager.paint(g, 0, 0);
    
    if( copterDead == true){
   
}

    copter.nextFrame();
    if(copter.collidesWith(backGround,true)){
        copterDead = true;
        
                                                            
            try{
                     Thread.sleep(10);
            }
            catch(InterruptedException e){}
            if (directionDown == true){         
            y++;
            }
            else {
              y--;
            }
            copter.setRefPixelPosition(xViewPort + 35, y+35);
            repaint();
        }
        
        public void keyPressed(int keyCode) {
    if (keyCode == KEY_NUM0) { 
        directionDown = false; 
    }
   }
   
  public void maakAchterGrond(){
    
        try {
            backGroundTiles = Image.createImage("/tiles.png");
        }
        catch(Exception e) {
            backGroundTiles = null;
        }
        backGround = new TiledLayer(50, 12, backGroundTiles,15, 15) ; // 9 colums of 9 tiles of 15x15
        
        backGround.fillCells(0,0,10,1,11); //column-number, row-number, number of columns, amount of row tiles, tile-type 
     
      backGround.setCell(10,0,3); //column, row, tile-type
      backGround.setCell(11,0,3);
      backGround.setCell(11,1,3);
      backGround.setCell(11,2,3);
      backGround.setCell(12,0,3);
      backGround.setCell(12,1,3);
      backGround.setCell(12,2,3);
      backGround.setCell(12,3,3);
      backGround.setCell(10,1,1);
        backGround.setCell(11,2,1);
        backGround.setCell(12,3,1);
        backGround.setCell(13,3,11);
        backGround.setCell(13,2,3);
        backGround.setCell(13,1,3);
        backGround.setCell(13,9,7);
        backGround.setCell(12,10,7);
        backGround.setCell(14,3,11);
        backGround.setCell(14,2,3);
        backGround.setCell(14,1,3);
        backGround.setCell(15,2,2);
        backGround.setCell(15,1,3);
        backGround.setCell(16,1,3);
        backGround.setCell(16,2,1);
        backGround.setCell(17,3,6);
        backGround.setCell(14,8,9);
        backGround.setCell(14,9,3);
        backGround.setCell(14,10,3);
        backGround.setCell(15,9,8);
        backGround.setCell(15,10,3);
        backGround.setCell(16,9,7);
        backGround.setCell(16,10,3);
        backGround.setCell(17,8,7);
        backGround.setCell(17,9,3);
        backGround.setCell(17,10,3);
        backGround.setCell(18,7,7);
        backGround.setCell(18,8,3);
        backGround.setCell(18,9,3);
        backGround.setCell(18,10,3);
        backGround.setCell(19,7,10);
        backGround.setCell(19,8,3);
        backGround.setCell(19,9,3);
        backGround.setCell(19,10,3);
        backGround.setCell(20,7,10);
        backGround.setCell(20,8,3);
        backGround.setCell(20,9,3);
        backGround.setCell(20,10,3);

        //backGround.setCell(0,10,3); column-number, row-number, number of columns, amount of row tiles, tile-type 
        backGround.fillCells(0,11,12,1,10);
        backGround.fillCells(12,11,12,1,3);
    }

    
   public void keyReleased(int keyCode) {
    if (keyCode == KEY_NUM0) {
        directionDown = true;
   }
  }
}


Het stukje code

code:
1
2
3
if( copterDead == true){
   
}


is dus de sleutel, maar ik ben de kluts kwijt hoe ik dit het beste aan moet pakken.

-[ Het Kwartjuh Valt... ]-


Acties:
  • 0 Henk 'm!

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 19-09 21:24

.oisyn

Moderator Devschuur®

Demotivational Speaker

Als je nou kijkt naar regel 19 en 36, gaat er dan niet een lichtje branden? Ik bedoel, blijkbaar snapte je daar ook al wat je aan het doen was :)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Acties:
  • 0 Henk 'm!

Verwijderd

Kijk inderdaad even naar de framesequence. Je wilt dat hij de derde frame (van de kapotte heli) laat zien als de helikopter ook echt kapot is (dus copterDead == true).

Acties:
  • 0 Henk 'm!

  • Kwartjuh
  • Registratie: Mei 2002
  • Laatst online: 25-08 12:54
Ok, je hebt gelijk. Dit project heeft een jaar stil gestaan dus ik moest er weer ff goed induiken. Maar het werkt, dus thanks! :)

-[ Het Kwartjuh Valt... ]-


Acties:
  • 0 Henk 'm!

  • bredend
  • Registratie: September 2001
  • Laatst online: 18-09 21:45
offtopic:
Zo, het Kwartjuh is gevallen ;)