http://bbs.9ria.com/thread-61328-1-1.html
//绘制一个菱形地图的代码
View Code
package com.joyairport.model{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.geom.Point;
import flash.events.MouseEvent;
import org.puremvc.as3.core.Model;
public class BaseMap {
private var MapWidth: Number; //760x2=1520
private var MapHeight: Number; //680x2=1360
private var TileWidth :Number; //tile=60
private var TileHeight :Number; //tile=30
private var HalfTileWidth:int;
private var HalfTileHeight:int;
public function BaseMap() {
// constructor code
//drawBaseMap(1520, 1360, 60, 30);
}
//绘制地图
public function drawBaseMap(mapWidth:int, mapHeight:int, tileWidth:int, tileHeight:int):DisplayObject {
this.MapWidth = mapWidth;
this.MapHeight = mapHeight;
this.TileWidth = tileWidth;
this.TileHeight = tileHeight;
var col:int = this.MapWidth / this.TileWidth;
var row:int = this.MapHeight / this.TileHeight;
this.HalfTileWidth = int(this.TileWidth / 2);
this.HalfTileHeight = int(this.TileHeight / 2);
trace("mapWidth = " + mapWidth + " __ row = " + row);
trace("mapHeight = " + mapHeight + " __ col = " + col);
var grid:Shape = new Shape();
grid.graphics.lineStyle(1, 0xC3C3C3, 1);
var dblMapWidth:int = col * 2 + 1;
var dblMapHeight:int = row * 2 + 1;
for (var i:int = 1; i < dblMapWidth; i = i + 2 ) {
grid.graphics.moveTo(i * this.HalfTileWidth, 0);
if (dblMapHeight + i >= dblMapWidth) {
grid.graphics.lineTo(dblMapWidth * this.HalfTileWidth, (dblMapWidth - i) * this.HalfTileHeight);
}else {
grid.graphics.lineTo((dblMapHeight + i) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
grid.graphics.moveTo(i * this.HalfTileWidth, 0);
if (i <= dblMapHeight) {
grid.graphics.lineTo(0, i * this.HalfTileHeight);
}else {
grid.graphics.lineTo((i - row - 1) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
}
for (var j:int = 1; j < dblMapHeight; j = j + 2) {
grid.graphics.moveTo(0, j * this.HalfTileHeight);
if (dblMapHeight - j >= dblMapWidth) {
grid.graphics.lineTo(dblMapWidth * this.HalfTileWidth, (dblMapWidth + j) * this.HalfTileHeight);
}else {
grid.graphics.lineTo((dblMapHeight - j) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
}
/*
for (var m:int = 0; m < dblMapHeight; m = m + 2) {
grid.graphics.moveTo(dblMapWidth * this.HalfTileWidth, m * this.HalfTileHeight);
if (dblMapWidth - dblMapHeight + m < 0) {
grid.graphics.lineTo(0, (dblMapWidth + m) * this.HalfTileHeight);
}else {
grid.graphics.lineTo((dblMapWidth - dblMapHeight + m) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
}
*/
return grid;
}
}
}
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.geom.Point;
import flash.events.MouseEvent;
import org.puremvc.as3.core.Model;
public class BaseMap {
private var MapWidth: Number; //760x2=1520
private var MapHeight: Number; //680x2=1360
private var TileWidth :Number; //tile=60
private var TileHeight :Number; //tile=30
private var HalfTileWidth:int;
private var HalfTileHeight:int;
public function BaseMap() {
// constructor code
//drawBaseMap(1520, 1360, 60, 30);
}
//绘制地图
public function drawBaseMap(mapWidth:int, mapHeight:int, tileWidth:int, tileHeight:int):DisplayObject {
this.MapWidth = mapWidth;
this.MapHeight = mapHeight;
this.TileWidth = tileWidth;
this.TileHeight = tileHeight;
var col:int = this.MapWidth / this.TileWidth;
var row:int = this.MapHeight / this.TileHeight;
this.HalfTileWidth = int(this.TileWidth / 2);
this.HalfTileHeight = int(this.TileHeight / 2);
trace("mapWidth = " + mapWidth + " __ row = " + row);
trace("mapHeight = " + mapHeight + " __ col = " + col);
var grid:Shape = new Shape();
grid.graphics.lineStyle(1, 0xC3C3C3, 1);
var dblMapWidth:int = col * 2 + 1;
var dblMapHeight:int = row * 2 + 1;
for (var i:int = 1; i < dblMapWidth; i = i + 2 ) {
grid.graphics.moveTo(i * this.HalfTileWidth, 0);
if (dblMapHeight + i >= dblMapWidth) {
grid.graphics.lineTo(dblMapWidth * this.HalfTileWidth, (dblMapWidth - i) * this.HalfTileHeight);
}else {
grid.graphics.lineTo((dblMapHeight + i) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
grid.graphics.moveTo(i * this.HalfTileWidth, 0);
if (i <= dblMapHeight) {
grid.graphics.lineTo(0, i * this.HalfTileHeight);
}else {
grid.graphics.lineTo((i - row - 1) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
}
for (var j:int = 1; j < dblMapHeight; j = j + 2) {
grid.graphics.moveTo(0, j * this.HalfTileHeight);
if (dblMapHeight - j >= dblMapWidth) {
grid.graphics.lineTo(dblMapWidth * this.HalfTileWidth, (dblMapWidth + j) * this.HalfTileHeight);
}else {
grid.graphics.lineTo((dblMapHeight - j) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
}
/*
for (var m:int = 0; m < dblMapHeight; m = m + 2) {
grid.graphics.moveTo(dblMapWidth * this.HalfTileWidth, m * this.HalfTileHeight);
if (dblMapWidth - dblMapHeight + m < 0) {
grid.graphics.lineTo(0, (dblMapWidth + m) * this.HalfTileHeight);
}else {
grid.graphics.lineTo((dblMapWidth - dblMapHeight + m) * this.HalfTileWidth, dblMapHeight * this.HalfTileHeight);
}
}
*/
return grid;
}
}
}
//
//用小菱形块拼接的Rhombus地图,45度角的
View Code
//package com.joyairport.model
{
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.SpreadMethod;
import flash.display.Sprite;
/**
* ...
* @author guolichun
*/
public class RhombusMap
{
/* private var rhombus_Number = 80;
private var rhombus_height:Number = 40;
private var rhombus_pointx:Number = 0;
private var rhombus_pointy:Number = 0;
private var map_Number = 1024;
private var map_height:Number = 1024;*/
public function RhombusMap() {
}
/**
* 绘制一个菱形
* @param _point_x = 起点坐标x
* @param _point_y = 起点坐标y
* @param _rhombus_width = 小菱形所在矩形的宽度
* @param _rhombus_height = 小菱形所在矩形的高度
* @return
*/
public function drawRhombus(_point_x:Number, _point_y:Number, _rhombus_Number, _rhombus_height:Number):Sprite {
var r:Sprite = new Sprite();
var s:Shape = new Shape();
s.graphics.lineStyle(1, 0x000000);
s.graphics.beginFill(0x00666CC, 0.3);
s.graphics.moveTo(_point_x, _point_y);
s.graphics.lineTo(_point_x + _rhombus_height, _point_y + (_rhombus_height / 2));
s.graphics.lineTo(_point_x, _point_y + _rhombus_height);
s.graphics.lineTo(_point_x - _rhombus_height, _point_y + (_rhombus_height / 2));
//s.graphics.lineTo(rh, px);
s.graphics.endFill();
r.addChild(s);
return r;
}
/**
* 绘制菱形地图
* @param rw = rhombus宽度
* @param rh = rhombus高度
* @param mw = map宽度
* @param mh = map高度
* @return
*/
public function drawMap(rw:Number,rh:Number,mw:Number,mh:Number):Sprite {
var grid:Sprite = new Sprite();
for (var i:int = 0; i < (mw / rw); i++ ) {
for (var j:int = 0; j < (mh / rh); j++ ) {
var rhombus:Sprite = drawRhombus(rw * i + rw, rh*j + rh, rw, rh);
trace("i = " + i + " j = " + i);
grid.addChild(rhombus);
}
}
return grid;
}
}
}
{
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.SpreadMethod;
import flash.display.Sprite;
/**
* ...
* @author guolichun
*/
public class RhombusMap
{
/* private var rhombus_Number = 80;
private var rhombus_height:Number = 40;
private var rhombus_pointx:Number = 0;
private var rhombus_pointy:Number = 0;
private var map_Number = 1024;
private var map_height:Number = 1024;*/
public function RhombusMap() {
}
/**
* 绘制一个菱形
* @param _point_x = 起点坐标x
* @param _point_y = 起点坐标y
* @param _rhombus_width = 小菱形所在矩形的宽度
* @param _rhombus_height = 小菱形所在矩形的高度
* @return
*/
public function drawRhombus(_point_x:Number, _point_y:Number, _rhombus_Number, _rhombus_height:Number):Sprite {
var r:Sprite = new Sprite();
var s:Shape = new Shape();
s.graphics.lineStyle(1, 0x000000);
s.graphics.beginFill(0x00666CC, 0.3);
s.graphics.moveTo(_point_x, _point_y);
s.graphics.lineTo(_point_x + _rhombus_height, _point_y + (_rhombus_height / 2));
s.graphics.lineTo(_point_x, _point_y + _rhombus_height);
s.graphics.lineTo(_point_x - _rhombus_height, _point_y + (_rhombus_height / 2));
//s.graphics.lineTo(rh, px);
s.graphics.endFill();
r.addChild(s);
return r;
}
/**
* 绘制菱形地图
* @param rw = rhombus宽度
* @param rh = rhombus高度
* @param mw = map宽度
* @param mh = map高度
* @return
*/
public function drawMap(rw:Number,rh:Number,mw:Number,mh:Number):Sprite {
var grid:Sprite = new Sprite();
for (var i:int = 0; i < (mw / rw); i++ ) {
for (var j:int = 0; j < (mh / rh); j++ ) {
var rhombus:Sprite = drawRhombus(rw * i + rw, rh*j + rh, rw, rh);
trace("i = " + i + " j = " + i);
grid.addChild(rhombus);
}
}
return grid;
}
}
}
//拖动容器的代码sprite
View Code
//stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMovefun);
stage.addEventListener(MouseEvent.CLICK,onclickfun);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onmousedownfun);
stage.addEventListener(MouseEvent.MOUSE_UP,onmouseupfun);
function onmouseupfun(e:MouseEvent):void{
trace("mouse UP");
this.stopDrag();
}
function onmousedownfun(e:MouseEvent):void{
trace("mouse DOWN ");
this.startDrag();
}
function onMouseMovefun(e:MouseEvent):void
{
//trace("_"+ e.localX + "_" + e.localY);
}
function onclickfun(e:MouseEvent):void
{
trace("CLICK==>_X="+ e.stageX + "_Y=" + e.stageY + "__local:_" + e.localX + "_" + e.localY);
}
stage.addEventListener(MouseEvent.CLICK,onclickfun);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onmousedownfun);
stage.addEventListener(MouseEvent.MOUSE_UP,onmouseupfun);
function onmouseupfun(e:MouseEvent):void{
trace("mouse UP");
this.stopDrag();
}
function onmousedownfun(e:MouseEvent):void{
trace("mouse DOWN ");
this.startDrag();
}
function onMouseMovefun(e:MouseEvent):void
{
//trace("_"+ e.localX + "_" + e.localY);
}
function onclickfun(e:MouseEvent):void
{
trace("CLICK==>_X="+ e.stageX + "_Y=" + e.stageY + "__local:_" + e.localX + "_" + e.localY);
}
//
//
//
//