0
내 게임을 시작하기위한 코드를 작성했습니다. 마우스에서 터치 이벤트로 컨트롤을 변경하여 내 ipad에서 사용할 수있게하려고합니다. 아래AS3 코드를 마우스에서 ios로 변환 터치 이벤트
코드는 내가 가지고있다하지만 난 그것을 터치로 작동하고
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
public class Main extends Sprite {
private var world:b2World;
private var worldScale:Number = 30;
private var mouseJoint:b2MouseJoint;
Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;
var myBlock:Array = new Array(
new Block(),
new Block(),
new Block()
);
public function Main() {
addChild(myBlock[0]);
addChild(myBlock[1]);
addChild(myBlock[2]);
world = new b2World(new b2Vec2(0,9.81),true);
debugDraw();
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,470/worldScale);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.friction = 1;
fixtureDef.restitution = 0.5;
fixtureDef.shape = polygonShape;
var groundBody:b2Body = world.CreateBody(bodyDef);
groundBody.CreateFixture(fixtureDef);
for (var i:int=0; i<3; i++) {
createBox(Math.random()*500+70,400,b2Body.b2_dynamicBody);
var targetBox:TargetBox=new TargetBox();
addChild(targetBox);
targetBox.x = 220 + i * 100;
targetBox.y = 180;
}
addEventListener(Event.ENTER_FRAME,updateWorld);
stage.addEventListener(TouchEvent.TOUCH_BEGIN, createJoint);
}
private function createBox(pX:Number,pY:Number,type:int):void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
bodyDef.type = type;
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(30/worldScale,30/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape = polygonShape;
fixtureDef.density = 1;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.2;
var box:b2Body = world.CreateBody(bodyDef);
box.CreateFixture(fixtureDef);
}
private function createJoint(e:TouchEvent):void {
world.QueryPoint(queryCallback,mouseToWorld());
}
private function queryCallback(fixture:b2Fixture):Boolean {
var touchedBody:b2Body = fixture.GetBody();
if (touchedBody.GetType() == b2Body.b2_dynamicBody) {
var jointDef:b2MouseJointDef=new b2MouseJointDef();
jointDef.bodyA = world.GetGroundBody();
jointDef.bodyB = touchedBody;
jointDef.target = mouseToWorld();
jointDef.maxForce = 1000 * touchedBody.GetMass();
mouseJoint = world.CreateJoint(jointDef) as b2MouseJoint;
stage.addEventListener(TouchEvent.TOUCH_MOVE, moveJoint);
stage.addEventListener(TouchEvent.TOUCH_END, finish);
stage.addEventListener(TouchEvent.TOUCH_END,killJoint);
}
return false;
}
private function moveJoint(e:TouchEvent):void {
mouseJoint.SetTarget(mouseToWorld());
}
private function killJoint(e:TouchEvent):void {
world.DestroyJoint(mouseJoint);
mouseJoint = null;
stage.removeEventListener(TouchEvent.TOUCH_MOVE,moveJoint);
stage.removeEventListener(TouchEvent.TOUCH_END,killJoint);
stage.removeEventListener(TouchEvent.TOUCH_END,finish);
}
private function mouseToWorld():b2Vec2 {
return new b2Vec2(mouseX/worldScale,mouseY/worldScale);
}
private function debugDraw():void {
var debugDraw:b2DebugDraw=new b2DebugDraw();
var debugSprite:Sprite=new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);
debugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(debugDraw);
}
private function finish(e:TouchEvent):void {
for (var b:b2Body=world.GetBodyList(); b; b=b.GetNext()) {
for (var i:int=0; i<3; i++) {
var distX:Number = b.GetPosition().x * worldScale - (220 + i * 100);
var distY:Number = b.GetPosition().y * worldScale - 180;
if (distX*distX+distY*distY<900 && b.GetType()==b2Body.b2_dynamicBody) {
world.DestroyBody(b);
createBox(220 + i * 100,180,b2Body.b2_staticBody);
}
}
}
}
private function updateWorld(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces();
var m=0;
for (var b:b2Body=world.GetBodyList(); b; b=b.GetNext()) {
if (m==3) {m=0;}
var distX:Number = b.GetPosition().x * worldScale - (220 + m * 100);
var distY:Number = b.GetPosition().y * worldScale - 180;
if (b.GetType()==b2Body.b2_dynamicBody) {
myBlock[m].x = distX + (220 + m * 100);
myBlock[m].y = distY + 180;
myBlock[m].rotation = b.GetAngle() * 180/Math.PI;
}
m++
}
world.DrawDebugData();
}
}
}
더 구체적으로 설명하면 어떻게 작동합니까? 오류가 있습니까? – BadFeelingAboutThis
@LondonDrugs_MediaServices 질문 외에도 마우스 대신 터치가 필요한지보고 싶습니다. 마우스 이벤트는 iPad 및 기타 장치에서 터치로 트리거되며 비 터치 플랫폼에서도 정상적으로 작동한다는 장점이 있습니다. 유일한 장점 터치 이벤트는 멀티 터치 지원을 제공한다는 것이지만 실제로는 그렇게 많이 필요하지 않습니다. –