FUNDAMENTUMEin pädagogisches Forum | 
|
| 
 | 
RE: Flash AS3: Ein Würfelspiel
in Web, Graphik und Multimedia 15.03.2013 10:30von tommaier • 205 Beiträge
Der Code könnte wie folgt aussehen:
var wurf:Number;
var meinTimer:Timer=new Timer(1000,5);
var ausgabe:String="";
var zaehl:int=0;
var i:int = 0;
var posX:int = 0;
var posY:int = 0;
var objekname:String;
//SHARED OBJEKT ERSTELLUNG ==============================================================
//zum Speichern und Laden des letzten Wurfes
var so:SharedObject=SharedObject.getLocal("lwurf");
if (so.size==0) {trace("Noch kein Cookie vorhanden")}
    else {auslesen();}
function speichern():void {
	so.data.score=ausgabe;
	so.flush();}
function auslesen():void {
	this.txt_zuletzt.text=so.data.score;}
//=======================================================================================
//initialisieren der Würfel
neusetzen();
//eventlistener für den Button
this.btn_wurf.addEventListener(MouseEvent.CLICK,clickWuerfel);
meinTimer.addEventListener(TimerEvent.TIMER, timerUpdate);
//BUTTONFUNKTION ========================================================================
function clickWuerfel(evt:MouseEvent):void {
    //Es wird geprüft ob schon einmal gewürfelt wurde -----------------------------------
		if (zaehl == 5) {
		this.txt_aktuell.text="";
		this.txt_zuletzt.text=ausgabe.toString();
		this.txt_wurfnr.text="";
		ausgabe="";
		zaehl =0;
		meinTimer.reset();}
	//----------------------------------------------------------------------------------
	meinTimer.start();
	
}
//=======================================================================================
//ZUFALLSFUNKTION =======================================================================
//Für den Würfel und die xy-Koordinaten
function zufall(tief:Number, hoch:Number):Number {
	var zahl:Number=Math.random()*(hoch - tief) + tief;
	zahl=Math.round(zahl);
	trace(zahl);
	return zahl;
	}
//=======================================================================================
//TIMERFUNKTION =========================================================================
function timerUpdate(event:TimerEvent):void {
	neusetzen();
	wurf=zufall(1,6);//aufruf der wuerfel-funktion
	ausgabe += wurf.toString() + ", ";
	this.txt_aktuell.text=ausgabe.toString();//aufruf der Anzeigefunktion
	zaehl +=1;
	this.txt_wurfnr.text = zaehl.toString();
	speichern();
	//Per Zufall eine Position für den Würfel finden
	posX = zufall(100, 600);
	posY = zufall(150, 350);
	
// WÄRE TOLL WENN ES SO FUNKTIONIERT	
//	this.ww("wurf").x=posX;
//	this.ww'wurf'.y=posY;
//	this.objektname.visible=true;
   switch (wurf) {
		case 1:
		this.ww1.x=posX;
		this.ww1.y=posY;
    	this.ww1.visible=true;
		break;
		
		case 2:
		this.ww2.x=posX;
		this.ww2.y=posY;
    	this.ww2.visible=true;
		break;
		
		case 3:
		this.ww3.x=posX;
		this.ww3.y=posY;
    	this.ww3.visible=true;
		break;
		
		case 4:
		this.ww4.x=posX;
		this.ww4.y=posY;
    	this.ww4.visible=true;
		break;
		
		case 5:
		this.ww5.x=posX;
		this.ww5.y=posY;
    	this.ww5.visible=true;
		break;
		
		case 6:
		this.ww6.x=posX;
		this.ww6.y=posY;
    	this.ww6.visible=true;
		break;
		
		
}
	
	
}
//=======================================================================================
//WÜRFEL VERSCHWINDEN FUNKTION ============================================================
function neusetzen():void {
this.ww1.visible=false;
this.ww2.visible=false;
this.ww3.visible=false;
this.ww4.visible=false;
this.ww5.visible=false;
this.ww6.visible=false;
}
//=======================================================================================
Dateianlage:
---
Wer USA sagt, muss auch USB sagen
| 
 | 
  Forum Statistiken
Das Forum hat 188
Themen
und
306
Beiträge.
 
 | 
   | Einfach ein eigenes Xobor Forum erstellen | 
Antworten
 Besucher
 Forum Statistiken