좋아, 어젯밤부터 이걸 고민해 왔습니다. 새 객체 (새 단위)를 만들고이를 화면에 그려 넣으면 (Flash는 모든 작업을 수행합니다) 처음 배치 된 지점이 원래 위치로 간주됩니다. 즉, getX와 getY는 그 점으로부터 거리를 반환합니다 ...어떻게 플래시 모양 (x, y) 좌표를 시작점이 아니라 프레임의 위치와 연관시킬 수 있습니까?
왜 그럴까요? 아니면 어떻게 멈출 수 있습니까?
function createUnit(health:int, attack:int, reach:int, speed:int, friendly:Boolean, type:String):Object {
var unit = new Object();
unit.cHealth = health;
unit.tHealth = health;
unit.uAttack = attack;
unit.uReach = reach;
unit.uSpeed = speed;
if (friendly) {
unit.xSpeed = -speed;
} else {
unit.xSpeed = speed;
}
unit.ySpeed = 0;
unit.isFriendly = friendly;
unit.unitType = type;
unit.updateUnit = function() {
if (unit.cHealth <= 0) {
deleteUnit(unit);
}
unit.moveUnit();
unit.findClosestUnit();
}
unit.getY = function():int {
return unit.shape.y;
}
unit.getX = function():int {
return unit.shape.x;
}
unit.moveUnit = function() {
unit.shape.x += unit.xSpeed;
unit.shape.y += unit.ySpeed;
}
unit.findClosestUnit = function() {
var dist:int = 0;
var closestDist:int = 0;
var closestUnit:Object = null;
var array:Array;
if (unit.isFriendly) {
array = eUnits;
} else {
array = fUnits;
}
if (array != null) {
var i:int = 0;
for (i; i < array.length; i++) {
var obj = array[i];
trace(this.shape.x);
dist = Math.sqrt(Math.pow(obj.getX() + unit.getX(), 2) + Math.pow(obj.getY() + unit.getY(), 2));
//trace(dist);
if (dist <= closestDist) {
closestDist = dist;
closestUnit = obj;
//basically the two lines below *will* start the unit's movement towards the nearest enemy.
//unit.ySpeed = (unit.getY() - closestUnit.getY())/unit.uSpeed;
//unit.xSpeed = (unit.getX() - closestUnit.getX())/unit.uSpeed;
}
if (dist <= unit.reach) {
unit.attackUnit(closestUnit);
}
}
}
}
unit.attackUnit = function(unit2:Object) {
unit2.cHealth -= unit.attack;
}
var spawnX:int;
var spawnY:int;
var color:uint;
if (friendly) {
spawnX = stage.stageWidth;
spawnY = stage.stageHeight/2;
color = 0xFFCC00;
} else {
spawnX = 0;
spawnY = Math.random() * stage.stageHeight;
color = 0x682388;
}
var tShape:Shape = new Shape();
tShape.graphics.beginFill(color);
tShape.graphics.lineStyle(2, 2);
tShape.graphics.drawCircle(spawnX, spawnY, 10);
tShape.graphics.endFill();
stage.addChild(tShape);
unit.shape = tShape;
return unit;
}
[Object Oriented Programming] (http://www.adobe.com/products/catalog.html)에 대해 읽었습니까? – Marty
그게 내가하고있는 일이야. 어제 플래시가 시작되었지만 자바를 배경으로. –
자바가 무겁다 OOP는 AS3과 마찬가지로 왜 이런 함수를 중첩시킬 것인가? 귀하의 범위는 미친 듯이 관리하기 어려울 것입니다. 적절한 경로를 시작하기 위해 문서 클래스를 조사하는 것이 좋습니다. 불쾌감은 없지만이 코딩 스타일은 끔찍합니다. 표준 OOP를 따르지 않았기 때문에 대답을 얻지 못할지 의심 스럽습니다. 원점은 일반적으로 찾고있는 요소가 포함 된 경계 상자의 왼쪽 위 모퉁이입니다. –