Go Back   Random Stuff Forums > Technical > Programming

Programming Discuss any type of programming here.

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 06-08-2009, 02:21 AM
Matt's Avatar
(Offline)
Administrator
 
Join Date: Jan 2007
Location: United States - Ohio
Posts: 3,820
Matt is an unknown quantity at this point
Default [FlexBuilder / ActionScript 3.0] Chess Timer

This is the source code for a simple chess timer (two players with alternating times) written in ActionScript 3.0 on the FlexBuilder platform.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import flash.events.*;
			import mx.controls.*;
			
			[Bindable]
			public var time1:String = "00:00";
			[Bindable]
			public var time2:String = "00:00";
			
			public var time1Front:int = 0;
			public var time1Rear:int = 0;
			public var time2Front:int = 0;
			public var time2Rear:int = 0;
			public var mainTimer:Timer = new Timer(1000);
			public var turn:Boolean = false;
			public var change:Boolean = false;
			
			private function startF():void 
			{
				if (!change)
				{
				mainTimer.start();
				mainTimer.addEventListener(TimerEvent.TIMER,timeChange);
				start.label = "Switch";
				change = true;
				} 
				else
				{
				turn = !turn;
				}
			}
			private function timeChange(e:TimerEvent):void
			{
				if (turn)
				{
					this.player1.setStyle("backgroundColor",0x00FF00);
					this.player2.setStyle("backgroundColor",0xFFFFFF);
					time1Rear++;
					if (time1Rear > 59)
					{
						time1Front++;
					}
					if (time1Front < 10)
					{
						time1 = "0" + time1Front.toString() + ":" + time1Rear.toString();
					}
					if (time1Rear < 10)
					{
						time1 = time1Front.toString() + ":0" + time1Rear.toString();
					}
					if (time1Front < 10 && time1Rear < 10)
					{
						time1 = "0" + time1Front.toString() + ":0" + time1Rear.toString();
					}
					if (time1Front > 10 && time1Rear > 10)
					{
						time1 = time1Front.toString() + ":" + time1Rear.toString();
					}				
				}
				else if (!turn)
				{
					this.player2.setStyle("backgroundColor",0x00FF00);
					this.player1.setStyle("backgroundColor",0xFFFFFF);
					time2Rear++;
					if (time2Rear > 59)
					{
						time2Front++;
					}
					if (time2Front < 10)
					{
						time2 = "0" + time2Front.toString() + ":" + time2Rear.toString();
					}
					if (time2Rear < 10)
					{
						time2 = time2Front.toString() + ":0" + time2Rear.toString();
					}
					if (time2Front < 10 && time2Rear < 10)
					{
						time2 = "0" + time2Front.toString() + ":0" + time2Rear.toString();
					}
					if (time2Front > 10 && time2Rear > 10)
					{
						time2 = time2Front.toString() + ":" + time2Rear.toString();
					}				
				}				
			}
			private function resetF():void
			{
				start.label = "Start";
				mainTimer.stop();
				change = false;
				this.player1.setStyle("backgroundColor",0xFFFFFF);
				this.player2.setStyle("backgroundColor",0xFFFFFF);
				time1 = "00:00";
				time2 = "00:00";
				time1Front = 0;
				time1Rear = 0;
				time2Front = 0;
				time2Rear = 0;
			}
		]]>
	</mx:Script>
	<mx:Button x="444" y="246" label="Start" click="startF();" id="start"/>
	<mx:Button x="583" y="246" label="Reset" click="resetF();" id="reset"/>
	<mx:Canvas x="199" y="37" width="300" height="170" id="player1" backgroundColor="#FFFFFF">
		<mx:Text x="106" y="10" text="Player 1" fontSize="20"/>
		<mx:Label x="85.5" y="49" text="{time1}" width="130" height="50" fontSize="20" textAlign="center"/>
	</mx:Canvas>
	<mx:Canvas x="583" y="37" width="300" height="170" id="player2" backgroundColor="#FFFFFF">
		<mx:Text x="106" y="10" text="Player 2" fontSize="20"/>
		<mx:Label x="85" y="49" text="{time2}" fontSize="20" textAlign="center" width="130" height="50"/>
	</mx:Canvas>
	<mx:Text x="380" y="336" text="Made and designed by Matthew McNally in ActionScript 3.0" width="330"/>
</mx:Application>
Live demo: http://checkgamertag.com/chess/
__________________
"Facts do not cease to exist because they are ignored."
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 04:58 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright © 2006 - 2010, Rsforums.org