flash 连连看小游戏地图数据简单生成的AS代码
程序员文章站
2022-06-07 19:59:26
这篇教程是向脚本之家的朋友flash 连连看小游戏地图数据简单生成的AS代码,教程很简单,推荐到脚本之家,喜欢的朋友可以过来看一下,希望大家喜欢... 14-12-31...
比较简单,这里不包括判断是否一定有解的部分,只是一个随意生成地图(给定行,列,每种图片的生成个数),没有思路的可以参考一下,高手也可以指正。
复制代码
代码如下://生成地图的类
package src.ww.llk.map
{
import mx.collections.arraycollection;
public class makemap
{
private var mapparam:mapparam = null;
private var types:arraycollection = null;
//保证取得的数据不被重取
private var flags:arraycollection = null;
public function makemap(param:mapparam):void {
mapparam = param;
initdata();
}
// 初始化必要的数据
private function initdata():void {
types = new arraycollection();
var typenum:int = math.floor(mapparam.cols*mapparam.rows/mapparam.numberpertype);
for(var i:int=1;i<=typenum;i++) {
for (var j:int=0;j<mapparam.numberpertype;j++) {
types.additem(i);
}
}
var yushu:int = mapparam.cols*mapparam.rows - typenum*mapparam.numberpertype;
for (var yi:int = 0; yi<yushu;yi++) {
types.additem(1);
}
}
//随机生成地图
public function make():array {
flags = new arraycollection();
for(var i:int=0;i<types.length;i++){
flags.additem(i);
}
var mapdata:array = new array();
var rowdata:array = null;
var col:int = 0;
rowdata = new array();
rowdata.push(0);
for(col = 0;col<mapparam.cols;col++){
rowdata.push(0);
}
rowdata.push(0);
mapdata.push(rowdata);
for(var row:int = 0;row<mapparam.rows;row++){
rowdata = new array();
rowdata.push(0);
for(col = 0;col<mapparam.cols;col++){
rowdata.push(gettype());
}
rowdata.push(0);
mapdata.push(rowdata);
}
rowdata = new array();
rowdata.push(0);
for(col = 0;col<mapparam.cols;col++){
rowdata.push(0);
}
rowdata.push(0);
mapdata.push(rowdata);
return mapdata;
}
// 随机取得单个数据
private function gettype():int {
var ran:int=-1;
var ret:int = 0;
ran = randomindex();
var index:int = int(flags.getitemat(ran));
flags.removeitemat(ran);
ret = int(types.getitemat(index));
return ret;
}
private function randomindex():int {
return math.random() * (flags.length - 1);
}
}
}
参数类,就是一个bean,
package src.ww.llk.map
{
public class mapparam
{
public var rows:number=0;
public var cols:number=0;
public var numberpertype:int=4;
}
}
测试类:
public function test():void {
var param:mapparam = new mapparam();
param.rows = 6;
param.cols = 6;
param.numberpertype = 4;
var maker:makemap = new makemap(param);
var map:array = maker.make();
for each(var rowdata:array in map) {
var rowstr:string = "";
for each(var cell:int in rowdata) {
rowstr += cell + " ";
}
trace(rowstr);
}
}
运行结果:package src.ww.llk.map
{
import mx.collections.arraycollection;
public class makemap
{
private var mapparam:mapparam = null;
private var types:arraycollection = null;
//保证取得的数据不被重取
private var flags:arraycollection = null;
public function makemap(param:mapparam):void {
mapparam = param;
initdata();
}
// 初始化必要的数据
private function initdata():void {
types = new arraycollection();
var typenum:int = math.floor(mapparam.cols*mapparam.rows/mapparam.numberpertype);
for(var i:int=1;i<=typenum;i++) {
for (var j:int=0;j<mapparam.numberpertype;j++) {
types.additem(i);
}
}
var yushu:int = mapparam.cols*mapparam.rows - typenum*mapparam.numberpertype;
for (var yi:int = 0; yi<yushu;yi++) {
types.additem(1);
}
}
//随机生成地图
public function make():array {
flags = new arraycollection();
for(var i:int=0;i<types.length;i++){
flags.additem(i);
}
var mapdata:array = new array();
var rowdata:array = null;
var col:int = 0;
rowdata = new array();
rowdata.push(0);
for(col = 0;col<mapparam.cols;col++){
rowdata.push(0);
}
rowdata.push(0);
mapdata.push(rowdata);
for(var row:int = 0;row<mapparam.rows;row++){
rowdata = new array();
rowdata.push(0);
for(col = 0;col<mapparam.cols;col++){
rowdata.push(gettype());
}
rowdata.push(0);
mapdata.push(rowdata);
}
rowdata = new array();
rowdata.push(0);
for(col = 0;col<mapparam.cols;col++){
rowdata.push(0);
}
rowdata.push(0);
mapdata.push(rowdata);
return mapdata;
}
// 随机取得单个数据
private function gettype():int {
var ran:int=-1;
var ret:int = 0;
ran = randomindex();
var index:int = int(flags.getitemat(ran));
flags.removeitemat(ran);
ret = int(types.getitemat(index));
return ret;
}
private function randomindex():int {
return math.random() * (flags.length - 1);
}
}
}
参数类,就是一个bean,
package src.ww.llk.map
{
public class mapparam
{
public var rows:number=0;
public var cols:number=0;
public var numberpertype:int=4;
}
}
测试类:
public function test():void {
var param:mapparam = new mapparam();
param.rows = 6;
param.cols = 6;
param.numberpertype = 4;
var maker:makemap = new makemap(param);
var map:array = maker.make();
for each(var rowdata:array in map) {
var rowstr:string = "";
for each(var cell:int in rowdata) {
rowstr += cell + " ";
}
trace(rowstr);
}
}
0 0 0 0 0 0 0 0
0 3 3 1 1 7 6 0
0 6 8 9 7 9 5 0
0 2 4 4 4 1 7 0
0 5 8 4 9 5 8 0
0 2 6 2 3 1 7 0
0 6 2 5 3 8 9 0
0 0 0 0 0 0 0 0
以上就是flash 连连看小游戏地图数据简单生成的as代码,谢谢大家阅读!