理想情况下,它必须在棋盘区域的随机位置上给我5个黑色方块。但实际上它给我的不到黑色方块。
我真的坚持这个,因为我真的找不到答案。
PS这里是编译的应用程序。 http://dl.dropbox.com/u/9288177/CSE/images/Conquester.swf
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" creationComplete="main()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import flash.display.Shape;
import flash.display.Sprite;
import mx.core.UIComponent;
import spark.core.SpriteVisualElement;
private var rectangle:Sprite = new Sprite();
private var insterter:UIComponent = new UIComponent();
private var theme:SpriteVisualElement = new SpriteVisualElement();
private var yH:int;
private var xH:int;
private var grid:Array = new Array();
const WIDTH:int = 20;
const HEIGHT:int = 20;
private function main():void
{
var uic:UIComponent = new UIComponent();
uic.width = uic.height = 50;
uic.graphics.lineStyle(1,0x888888);
//uic.graphics.
//uic.graphics.drawRect(0,0,50,50);
canvas.rawChildren.addChild(uic);
const boxW:int = 20;
const boxH:int = 20;
const planetnumber:int = 5;
for(yH = 0; yH < HEIGHT; yH++) {
var row:Array = new Array();
for (xH = 0; xH < WIDTH; xH++) {
uic.graphics.drawRect(boxW*yH,boxH*xH,boxW,boxH);
row.push(0);
}
grid.push(row);
}
var currentplanet:int = 0;
var rw:int;
var clmn:int;
var counter:int = 0;
while (currentplanet < planetnumber)
{
clmn= (Math.random() * (WIDTH-5)) + 5;
rw = (Math.random() * (HEIGHT-5)) + 5;
if (fieldcheck(rw, clmn) == true)
{
grid[rw][clmn] = 1;
uic.graphics.beginFill(0x000000, 1);
uic.graphics.drawRect(boxW*clmn,boxH*rw,boxW,boxH);
uic.graphics.endFill();
currentplanet++;
if (counter == 4)
{
c4.text=clmn.toString();
r4.text=rw.toString();
}
if (counter == 3)
{
c3.text=clmn.toString();
r3.text=rw.toString();
counter++;
}
if (counter == 2)
{
c2.text=clmn.toString();
r2.text=rw.toString();
counter++;
}
if (counter == 1)
{
c1.text=clmn.toString();
r1.text=rw.toString();
counter++;
}
if (counter == 0)
{
c0.text=clmn.toString();
r0.text=rw.toString();
counter++;
}
}
}
}
private function fieldcheck(iRow:int, iColumn:int):Boolean
{
if (grid[iRow][iColumn]==0)
{
if ((iRow > 0)&&(iRow < HEIGHT))
{
if ((iColumn > 0) && (iColumn < WIDTH))
{
if ((grid[iRow-1][iColumn-1]==0)&&(grid[iRow][iColumn-1]==0)&&(grid[iRow+1][iColumn-1]==0)&&(grid[iRow-1][iColumn]==0)&&(grid[iRow+1][iColumn]==0)&&(grid[iRow-1][iColumn+1]==0)&&(grid[iRow][iColumn+1]==0)&&(grid[iRow+1][iColumn+1]==0))
{
return true;
}
}
}
}
return false;
}
//var rnd:int = (Math.random()* 41) + 10;
/*if (rnd >= 48) {
uic.graphics.beginFill(0x000000, 1);
uic.graphics.drawRect(boxW*y,boxH*x,boxW,boxH);
uic.graphics.endFill();
}
else
{
uic.graphics.drawRect(boxW*y,boxH*x,boxW,boxH);
}*/
]]>
</fx:Script>
<mx:Canvas id="canvas" x="34" y="10" width="90%" height="90%" textAlign="center">
</mx:Canvas>
<s:Label id="r1" x="34" y="565" width="84" height="11" text="Label"/>
<s:Label id="r2" x="34" y="575" width="84" height="11" text="Label"/>
<s:Label id="c2" x="126" y="575" width="77" height="11" text="Label"/>
<s:Label id="c1" x="126" y="565" width="77" height="11" text="Label"/>
<s:Label id="r3" x="34" y="585" width="84" height="11" text="Label"/>
<s:Label id="c3" x="126" y="585" width="77" height="11" text="Label"/>
<s:Label id="r4" x="215" y="555" width="84" height="11" text="Label"/>
<s:Label id="c4" x="307" y="555" width="77" height="11" text="Label"/>
<s:Label id="r0" x="34" y="555" width="84" height="11" text="Label"/>
<s:Label id="c0" x="126" y="555" width="77" height="11" text="Label"/>
</s:Application>
答案 0 :(得分:2)
您的小应用程序的问题在于fieldcheck功能。您遇到的问题是,正如The_asMan所说,检查中发生的错误是它正在运行。问题是,当你在做一些边界检查时:
if ((iRow > 0)&&(iRow < HEIGHT))
if ((iColumn > 0) && (iColumn < WIDTH))
当您对iRow和iColumn值执行某些操作以查看相邻方块中是否包含项目时,这不包括在内。我将fieldcheck操作更改为以下内容,似乎可以解决这个问题:
private function fieldcheck(iRow:int, iColumn:int):Boolean
{
// I used looping here as it made the range checks simpler, plus you can
// extend the range of a boxes space out pretty easily by changing the
// - 1 and + 1 to a different number
for(var r:int = iRow - 1; r == iRow + 1; r++) {
if(r >= 0 && r < HEIGHT) {
for(var c:int = iColumn - 1; c == iColumn + 1; c++) {
if(c >= 0 && c < WIDTH) {
if(grid[r][c]==1) {
return false;
}
}
}
}
}
return true;
}
此外,请考虑更改以下行:
clmn= (Math.random() * (WIDTH-5)) + 5;
rw = (Math.random() * (HEIGHT-5)) + 5;
致:
clmn= (Math.random() * (WIDTH-5)) + 4;
rw = (Math.random() * (HEIGHT-5)) + 4;
这可以为数字提供更好的结果,因为数组是0索引的,而你生成的数字是5-20而不是4-19。 (我假设0-3的缓冲点是有意的)。
答案 1 :(得分:0)
这不是一个答案,而是一个很重要的评论作为评论发布 它工作正常,经过几次刷新后我看到了5个黑色正方形我得到了以下错误代码 当这个代码弹出时,我没有正方形。
TypeError: Error #1010: A term is undefined and has no properties.
at Conquester/fieldcheck()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:109]
at Conquester/main()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:55]
at Conquester/___Conquester_Application1_creationComplete()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:4]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at mx.core::UIComponent/set initialized()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:842]
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]