Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:11:35+00:00 2026-06-10T23:11:35+00:00

I have written some code to start off my game, i now want to

  • 0

I have written some code to start off my game, i now want to change the controls from mouse to touch event so i can use it on my ipad.

the code below is how far i have got but i can’t work out how to change it to work with touch and drag

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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:11:37+00:00Added an answer on June 10, 2026 at 11:11 pm

    All standard cursor and mouse events should translate to touchscreens. The obstacle is when you start using multitouch, but skimming through your code, it doesn’t yet look like you have anything that’s actually calling for multitouch data yet. Im not sure if the box2d libs require multitouch support as opposed to regular mouse events, but your errors could likely be coming from those libraries calling a “touch” related variable that may or may not be created yet, depending on where you are in your game. Are you in fact concerned with multi-support and what exactly are the errors you’re getting?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code for playing a .wav through my application. Now I
I have written some code to check two dates, a start date and an
guys I want to start programing with C++. I have written some programs in
I have written some code that loads an XML document using an XmlDocument object
I have written some code for displaying a drop down list, but the code
I have written some code in JSP to download .docx files which is hosted
I have written some code to ensure that items on an order are all
I have written some code to look at properties using reflection. I have retrieved
I have written some code to display a YouTube video on my page but
I have written some WebGL code, actually I am playing with the examples that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.