NEU: Alle Visual Codes als Code Snippets für Flash CS5:
jetzt hier downloaden .
Pong
Code Actionscript 2.0
Instanz-Aktion ball
onClipEvent (load) {
// set speed at the beginning
speedx = 5;
speedy = 12;
}
onClipEvent (enterFrame) {
// square moves with speedx, speedy
this._x = this._x + speedx;
this._y = this._y + speedy;
// if ball leaves stage, vertical change of direction
if ((this._y > 400) || (this._y < 0)) {
trace("ping");
speedy = -1 * speedy;
}
// if square hits b2/b3, horizontal change of direction
if (this.hitTest(_parent.racket1) || this.hitTest(_parent.racket2)) {
trace("pong");
speedx = -1 * speedx;
}
}
Instanz-Aktion racket1
onClipEvent (enterFrame) {
// racket follows vertical mouse position
this._y = _root._ymouse;
}
Code Actionscript 3.0
addEventListener(Event.ENTER_FRAME,enterFrame); var speedx=5; var speedy=12; function enterFrame(event:Event) { ball.x=ball.x+speedx; ball.y=ball.y+speedy; // if ball leaves stage, vertical change of direction if ((ball.y > 400) || (ball.y < 0)) { trace("ping"); speedy=-1*speedy; } // if ball hits b2/b3, horizontal change of direction if (ball.hitTestObject(racket1) || ball.hitTestObject(racket2)) { trace("pong"); speedx=-1*speedx; } racket1.y=mouseY; racket2.y=mouseY; }
Infos
Ein Ball wird von zwei Schlägern hin und hergespielt. Das Spiel Pong lässt sich durch Kombination der Skripts Maus Position, Abprallen und Kollision programmieren.
verwandt mit: Abprallen, Kollision, Maus Position
Download
Right click: Flashfile AS 2.0 | Flashfile AS 3.0 | SWF-File