Bon je sais que cette question a déja été posé. Mais je ne trouve quand même pas la solution dans MON code. voilà mon code :
Classe: SuperBloc
Classe Main
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 package ca.qc.cegepstefoy { import flash.display.Sprite; /** * ... * @author Jason */ public class SuperBloc extends Sprite { protected var application:Main protected var laCouleur:String; public function SuperBloc(app:Main, posX:Number, posY:Number, couleur:String) { this.application = app; this.laCouleur = couleur; this.x = x; this.y = y; this.dessinerBlocs(); } private function dessinerBlocs():void { if (this.laCouleur == "bleu") { this.graphics.beginFill(0x0000ff, 1); } else { this.graphics.beginFill(0xff0000, 1); } this.graphics.drawRect(0, 0, 20, 20); this.graphics.endFill(); this.application.addChild(this); } public function supprimerObjet():void { this.application.removeChild(this); } } }
Classe BlocsBleu
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 package ca.qc.cegepstefoy { import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; /** * ... * @author Jason */ public class Main extends Sprite { private var tBlocBleu:Array = new Array(); private var tBlocRouge:Array = new Array(); private var minuterieCourse:Timer = new Timer(1000, 0); public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point var bouton:Bouton = new Bouton(this) var modifierPosition:Number = 35; for (var cpt:int = 0; cpt <= 9; cpt++) { this.tBlocRouge[cpt] = new BlocsRouges(this, x, 60 + modifierPosition); this.tBlocBleu[cpt] = new BlocsBleus(this, 60 + modifierPosition, y, this.tBlocRouge); modifierPosition += 35; } } public function demarrerCourse():void { for (var cpt:int = 0; cpt<=9; cpt++) { this.tBlocBleu[cpt].deplacerBlocsBleus(); this.tBlocRouge[cpt].deplacerBlocsRouges(); } } public function enleverBlocsBleusTableau (refBlocsBleus:Sprite):void { var indice:int = this.tBlocBleu.indexOf(refBlocsBleus); this.tBlocBleu.splice (indice,1); } } }
Classe BlocsRouges
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 package ca.qc.cegepstefoy { import flash.events.TimerEvent; import flash.utils.Timer; /** * ... * @author Jason */ public class BlocsBleus extends SuperBloc { private static var vitesse:int=10; private var sens:int = -1; private var blocRouges:Array; private var minuterie:Timer = new Timer(100, 0); private static var minuterieVitesse:Timer = new Timer(1000,0) public function BlocsBleus(app:Main, leX:Number, leY:Number, refBlocRouges:Array) { super(app, x, y, "bleu") this.x = leX; this.y = 15; this.blocRouges = refBlocRouges; this.minuterie.addEventListener(TimerEvent.TIMER, deplacer); this.minuterie.addEventListener(TimerEvent.TIMER, detecterCollision); BlocsBleus.minuterieVitesse.addEventListener(TimerEvent.TIMER, changerVitesse); } public function deplacerBlocsBleus():void { this.minuterie.start(); BlocsBleus.minuterieVitesse.start(); } private function deplacer(e:TimerEvent):void { if (this.y > 500 || this.y<0) { this.sens *= -1; } this.y += BlocsBleus.vitesse * this.sens *-1; } private static function changerVitesse(e:TimerEvent):void { BlocsBleus.vitesse = Math.floor(Math.random() * 20 + 10); } private function detecterCollision(e:TimerEvent):void { var collisionBlocs:Boolean = false; for (var cpt:uint = 0; cpt < this.blocRouges.length && collisionBlocs == false; cpt++) { if (this.hitTestObject(this.blocRouges[cpt])){ //this.blocRouges[cpt].faireSupprimer(); this.supprimerBlocs(); } } } private function supprimerBlocs():void { this.minuterie.removeEventListener(TimerEvent.TIMER, deplacer); this.supprimerObjet(); this.application.enleverBlocsBleusTableau(this); } } }
Classe Bouton
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 package ca.qc.cegepstefoy { import flash.events.TimerEvent; import flash.utils.Timer; /** * ... * @author Jason */ public class BlocsRouges extends SuperBloc { private var vitesse:int; private var sens:int = -1; private var minuterie:Timer = new Timer(100, 0); private var minuterieVitesse:Timer = new Timer(1000,0) public function BlocsRouges(app:Main, leX:Number, leY:Number) { super(app, x, y, "rouge") this.x = 15; this.y = leY; this.minuterie.addEventListener(TimerEvent.TIMER, deplacer); this.minuterieVitesse.addEventListener(TimerEvent.TIMER, changerVitesse); } public function deplacerBlocsRouges():void { this.minuterie.start(); this.minuterieVitesse.start(); } private function deplacer(e:TimerEvent):void { if (this.x > 500 || this.x<0) { this.sens *= -1; } this.x += this.vitesse * this.sens * -1; } private function changerVitesse(e:TimerEvent):void { this.vitesse = Math.floor(Math.random() * 20 + 10); } } }
Le problème se produit lorsqu'il y a contact des deux blocs de couleur différente. Je veux qu'ils se suppriment une fois qu'ils se touchent.
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 package ca.qc.cegepstefoy { import flash.display.Sprite; import flash.events.MouseEvent; /** * ... * @author Jason */ public class Bouton extends Sprite { private var application:Main; public function Bouton(app:Main) { this.application = app; this.application.addChild(this); this.addEventListener(MouseEvent.MOUSE_DOWN, afficherDown); this.addEventListener(MouseEvent.MOUSE_UP, afficherUp); this.addEventListener(MouseEvent.CLICK, faireDemarrerCourse); this.afficherUp() this.x = 440; this.y=460 } private function afficherUp (evenement:MouseEvent = null):void { this.graphics.clear() this.graphics.beginFill(0x000ff, 1); this.graphics.drawRect(0, 0, 50, 20); this.graphics.endFill(); } private function afficherDown (evenement:MouseEvent):void { this.graphics.clear() this.graphics.beginFill(0xff000, 1); this.graphics.drawRect(0, 0, 50, 20); this.graphics.endFill(); } private function faireDemarrerCourse(e:MouseEvent):void { this.application.demarrerCourse(); } } }
Partager