悟到:编程语言是相通的。
一、前言
最近在 "拜师" 学Flash, 要求做出一个简单的"点点看"。
二、C#代码
因为刚学AS3.0,很多东东都不清楚,虽然在边查Adobe的HELP文档,但还是经常编译失败,很受打击==!忽然灵机一动,何不先用自己熟悉的C#来实现呢~
1、新建一个Winform;
2、拖拽3个控件:numericUpDown,button,richTextBox
3、添加Code:
C# Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace WindowsFormsApplication2
11![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
public partial class Form1 : Form
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
颜色 上一个点击的颜色;
15
int 上一个点击的序号 = -1;
16
bool 是否是第一次点击 = true;
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
List<颜色> 棋盘;
19
Random r;
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public Form1()
22![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
InitializeComponent();
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
private void button1_Click(object sender, EventArgs e)
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
int 现在点击的序号 = Convert.ToInt32(this.numericUpDown1.Value);
29
颜色 现在点击的颜色=棋盘[现在点击的序号];
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
if (是否是第一次点击)
32![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
上一个点击的颜色 = 现在点击的颜色;
34
上一个点击的序号 = 现在点击的序号;
35
是否是第一次点击 = false;
36
}
37
else
38![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
if (现在点击的颜色 == 上一个点击的颜色)
40![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
棋盘[上一个点击的序号] = 颜色.空;
42
棋盘[现在点击的序号] = 颜色.空;
43
}
44
是否是第一次点击 = true;
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
richTextBox1.Clear();
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
foreach (颜色 i in 棋盘)
50![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
richTextBox1.Text = richTextBox1.Text + i.ToString();
52
}
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
private void Form1_Load(object sender, EventArgs e)
56![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
棋盘 = new List<颜色>();
58
r =new Random();
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
for (int i = 0; i < 9; i++)
61![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
颜色 临时= (颜色)(r.Next(1,99999999)%3+1);
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
棋盘.Add(临时);
65
richTextBox1.Text = richTextBox1.Text+临时.ToString();
66
}
67
//dataGridView1.DataSource = 棋盘;
68
}
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
public enum 颜色
72![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
空 = 0,
74
红 = 1,
75
黄 = 2,
76
蓝 = 3
77
}
78
}
三、AS3 实现
1、新建一个Flash文档和AS文档--Click.as;
2、在场景中建一个元件,在库中打开该元件的链接属性,class命名为mc,这样我们就可以利用for循环,批量生成新的元件的实例,也是就批量复制的效果;
3、在Click.as中添加Code:
ActionScript Code
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package
{
2
import flash.display.MovieClip;
3
import flash.events.MouseEvent;
4
import flash.events.Event;
5
import flash.geom.ColorTransform;
6
import flash.geom.Transform;
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class Click extends MovieClip
{
8
private var widthNum:Number=3;
9
private var heightNum:Number=3;
10
private var arr_color:Array=new Array(0xffff00,0x000000,0xff0000,0xffffff);
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
private var color:ColorTransform;//上次点击的颜色
13
private var num:String;//上次点击的序号
14
private var isfirst:Boolean=true;//是否是第一次点击
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
private var color2:ColorTransform;
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public function Click():void
{
19
//setPath();
20
var n:int=0;
21![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i:int=0; i<widthNum; i++)
{
22![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var j:int=0; j<heightNum; j++)
{
23
var newMC:MovieClip=new mc();
24
newMC.name=String ("mc_"+n);
25
addChild(newMC);
26
n++;
27
newMC.x=j*60;
28
newMC.y=i*60;
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
var id:Number=Math.round(Math.random()*2);
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
var colorInfo:ColorTransform;
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
colorInfo = newMC.transform.colorTransform;
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
colorInfo.color=arr_color[id];
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
newMC.transform.colorTransform = colorInfo;
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
newMC.addEventListener(MouseEvent.CLICK,firstClickHandler);
41
}
42
}
43
//for (var m:int=0; m<9; m++) {
44
// getinfo("mc_"+m);
45
// var ss_color:ColorTransform=getChildByName("mc_"+m).transform.colorTransform;
46
// //trace(ss_color);
47
// }
48
}
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
//function setPath():void {
51
////for (var i:int=0; i<widthNum; i++) {
52
////for (var j:int=0; j<heightNum; j++) {
53
////newMC.name=String ("mc_"+n);
54
////addChild(newMC);
55
////n++;
56
////newMC.x=j*60;
57
////newMC.y=i*60;
58
////
59
////var id:Number=Math.round(Math.random()*2);
60
////
61
////var colorInfo:ColorTransform;
62
////
63
////colorInfo = newMC.transform.colorTransform;
64
////
65
////colorInfo.color=arr_color[id];
66
////
67
////newMC.transform.colorTransform = colorInfo;
68
////}
69
////}
70
////}
71
////
72
73![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function getinfo(mcname:String)
{
74
trace(mcname);
75
//trace(color+"---------------------");
76
}
77![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private function firstClickHandler(e:Event):void
{
78
//trace(">> 1e: " + e.type + " event from " + e.target.name + " called on " + e.target.transform.colorTransform);
79
var cur_num:String=e.target.name;
80
//var cur_num:int=int (cur_name.substr(3,1));
81
trace("cur_num:"+cur_num);
82
var cur_colorInfo:ColorTransform;
83
cur_colorInfo = e.target.transform.colorTransform;
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (isfirst)
{
86
color=cur_colorInfo;
87
num=cur_num;
88
isfirst=false;
89
//trace("------if------------"+color);
90![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
91
var color_int:uint=color.color;
92
var colorInfo_int:uint=cur_colorInfo.color;
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (color_int==colorInfo_int)
{
95
color2=getChildByName(cur_num).transform.colorTransform;
96
color2.color=arr_color[3];
97
getChildByName(cur_num).transform.colorTransform=color2;
98
getChildByName(num).transform.colorTransform=color2;
99
//trace("------else------------");
100
}
101
isfirst=true;
102
//trace("------else---else---------");
103
}
104
}
105
}
106
}