所以,我打算在我的CPPS内部建一个大房间,然而,我发现需要多个房间连在一起,有点烦人。我想知道我是否可以增加连接在一起的房间的大小,并使用WASD滚动它。
这是我想要管理的环境:
这是我要开发的岛屿的一部分,它是一幅粗略的画面,但你会发现它会继续向左移动。这就是我希望它滚动的地方。
尝试这样做我真的很疯狂吗?这甚至可能吗?任何评论都会有帮助!谢谢!
答案 0 :(得分:1)
您正在寻找的解决方案是一个蒙面容器。
myContainer.mask = myShape
为了好玩,我继续写了一个例子,你可以在一个干净的项目中运行。这应该展示地图平移概念。尝试使用WASD键,或只需移动鼠标。
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.events.KeyboardEvent;
// Our container for the map.
var container:Sprite;
// For the sake of smoothly animating to its destination, track the X & Y coordinates.
var destX:Number = 0;
var destY:Number = 0;
init();
function init():void {
// Create a backdrop (mostly for mouseMove)
var bg:Sprite = new Sprite();
bg.graphics.beginFill(0xFFFFFF);
bg.graphics.lineStyle(2, 0x00);
bg.graphics.drawRect(100, 100, stage.stageWidth - 200, stage.stageHeight - 200);
bg.graphics.endFill();
addChild(bg);
// Our container "map"
container = new Sprite();
addChild(container);
// Create a mask
var m:Shape = new Shape();
m.graphics.beginFill(0x00);
m.graphics.drawRect(0, 0, stage.stageWidth - 200, stage.stageHeight - 200);
m.graphics.endFill();
m.x = m.y = 100;
container.mask = m;
// Event Listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardMovement);
addEventListener("mouseMove", cursorMovement);
addEventListener("enterFrame", updateMap);
// Populate the map with shapes
for (var i:int = 0; i < 1000; i++) {
var c:Shape = createCircle();
container.addChild(c);
c.x = random(0, stage.stageWidth * 2); // Random X Coordinate
c.y = random(0, stage.stageHeight * 2); // Random Y Coordinate
}
}
function createCircle():Shape {
// Creates a randomly sized/colored circle
var c:Shape = new Shape();
c.graphics.beginFill(random(0x000000, 0xFFFFFF)); // Random Color
c.graphics.drawCircle(0,0,random(4,20)); // Random Size
c.graphics.endFill();
return c;
}
function keyboardMovement(e:KeyboardEvent):void {
var speed:int = 100
switch (e.charCode) {
case 115: // s:down
speed = -speed;
case 119: // w:up
destY = clamp(destY + speed, -this.loaderInfo.height, 0);
break;
case 100: // d:right
speed = -speed;
case 97: // a:left
destX = clamp(destX + speed, -this.loaderInfo.width, 0);
break;
}
}
function cursorMovement(e:Event):void {
destX = -mouseX;
destY = -mouseY;
}
function updateMap(e:Event):void {
container.x += (destX - container.x) * 0.1;
container.y += (destY - container.y) * 0.1;
}
function random(low:Number, high:Number):Number {
return Math.floor(Math.random() * (1+high-low)) + low;
}
function clamp(original:Number, low:Number, high:Number):Number {
return (original > high) ? high : (original < low) ? low : original;
}