How do I make a Box2D wrap around world? - libgdx

I want to make a wrap around game effect where an object would go off the screen from the x-axis one side and reappear in a new y-axis position on the other side of the screen. The width is 250 pixels, so basically it would pass (0, y1) and would reappear at (300, y2).
a.applyForceToCenter(aMovement, true);
a.applyTorque(3000, true);
FixtureDef fDef = new FixtureDef();
BodyDef ballD = new BodyDef();
ballD.type = BodyType.DynamicBody;
//random location for asteroid
int aLoc = (int) (aLocation * 15);
float x = 300;
switch(aLoc)
{
case 0:
ballD.position.set(x, -105);
break;
case 1:
ballD.position.set(x, -95);
break;
case 2:
ballD.position.set(x, -80);
break;
case 3:
ballD.position.set(x, -65);
break;
case 4:
ballD.position.set(x, -50);
break;
case 5:
ballD.position.set(x, -35);
break;
case 6:
ballD.position.set(x, -20);
break;
case 7:
ballD.position.set(x, -5);
break;
case 8:
ballD.position.set(x, 10);
break;
case 9:
ballD.position.set(x, 25);
break;
case 10:
ballD.position.set(x, 40);
break;
case 11:
ballD.position.set(x, 55);
break;
case 12:
ballD.position.set(x, 70);
break;
case 13:
ballD.position.set(x, 85);
break;
default:
ballD.position.set(x, 0);
}
PolygonShape asteroid = new PolygonShape();
asteroid.setAsBox(12.5f, 12.5f);
//asteroid definition
fDef.shape = asteroid;
fDef.density = .5f;
fDef.friction = .25f;
fDef.restitution = .75f;
a = world.createBody(ballD);
a.createFixture(fDef);
a.setFixedRotation(false);
//asteroid image
aSprite = new Sprite(new Texture("img/asteroid-icon.png"));
aSprite.setSize(12.5f * 4, 12.5f * 4);
aSprite.setOrigin(aSprite.getWidth() / 2, aSprite.getHeight() / 2);
a.setUserData(aSprite);
asteroid.dispose();

It might be possible to use a mod operator on your x and y positions. The idea being that you could mod whatever your x position and y position are with the pixel value of your box. That way when your x value = maxWidth, it becomes zero, and same for the y value.

You could use the setTransform() method on the ball if it leaves the screen on one side.
Basically, after each simulation step you'd check if the ball has been moved out on one side, then setTransform() with the new position and rotation.

Related

How to include MPEG codec?

Why embed video streams are not visible in my c++ program? I added WebBrowser into the program and the page that Browser should show is embed video stream. When I compile and run program, and when I press connect (button that should load web browser) it just loads black screen. That's when I use just embed video stream (valid one), when I use whole site where stream is, program loads whole site, expect stream which is black. What might be problem?
I tagged html and css in this post since they might know something about it too.
void__fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString code, key;
int br;
code = Edit1->Text;
if (code=="C577F") br=1;
if (code=="A9967") br=2;
if (code=="G02C1") br=3;
if (code=="TB4FA") br=4;
if (code=="U3E4B") br=5;
if (code=="KR11V") br=6;
if (code=="DFF9D") br=7;
if (code=="L738D") br=8;
if (code=="AA46C") br=9;
if (code=="EC6E7") br=10;
if (code=="T7BCE") br=11;
if (code=="JT429") br=12;
if (code=="R5F2C") br=13;
if (code=="O582B") br=14;
if (code=="A1FB4") br=15;
switch(br)
{
case 1:
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=1";
break;
case 2:
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=2";
break;
case 3:
key="3";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=3";
break;
case 4:
key="4";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=4";
break;
case 5:
key="5";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=5";
break;
case 6:
key="6";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=6";
break;
case 7:
key="7";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=7";
break;
case 8:
key="8";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=8";
break;
case 9:
key="9";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=9";
break;
case 10:
key="10";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=10";
break;
case 11:
key="11";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=11";
break;
case 12:
key="12";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=12";
break;
case 13:
key="13";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=13";
break;
case 14:
key="14";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=14";
break;
case 15:
key="15";
WebBrowser1->URL = "https://live-sports-stream.net/embed/video.php?channel=15";
break;
default:
ShowMessage("Code not valid. Contact #account on instagram for a new one.");
}
}

AS3 - TypeError: Error #1009 "null" at MethodInfo

I have this small project. Frames that change every time I click button, and a simulation of progress bar inside arrows.
stop();
/** initializtios **/
var trans:ColorTransform = next_arrow.transform.colorTransform;
var p:int = 1;
var myColorTransform = new ColorTransform();
myColorTransform.color = 0x42f498;
var trans1:ColorTransform = back_arrow.transform.colorTransform;
/** actual code **/
btn_next_arrow.addEventListener(MouseEvent.CLICK, function goNextLectFrame(e:MouseEvent):void {
if (p==1) {
fill_1.transform.colorTransform = myColorTransform;
fill_22.alpha = 0.00001;
fill_3.alpha = 0.00001;
fill_33.alpha = 0.00001;
fill_5.alpha = 0.00001;
fill_6.alpha = 0.00001;
fill_7.alpha = 0.00001;
fill_8.alpha = 0.00001;
fill_9.alpha = 0.00001;
fill_10.alpha = 0.00001;
fill_11.alpha = 0.00001;
fill_12.alpha = 0.00001;
fill_13.alpha = 0.00001;
fill_14.alpha = 0.00001;
fill_151.alpha = 0.00001;
++p;
}
switch (currentFrame) {
case 1:
gotoAndStop(2);
break;
case 2:
fill_22.transform.colorTransform = myColorTransform;
gotoAndStop(3);
break;
case 3:
fill_3.transform.colorTransform = myColorTransform;
gotoAndStop(4);
break;
case 4:
fill_33.transform.colorTransform = myColorTransform;
gotoAndStop(5);
break;
case 5:
fill_5.transform.colorTransform = myColorTransform;
gotoAndStop(6);
break;
case 6:
fill_6.transform.colorTransform = myColorTransform;
gotoAndStop(7);
break;
case 7:
fill_7.transform.colorTransform = myColorTransform;
gotoAndStop(8);
break;
case 8:
fill_8.transform.colorTransform = myColorTransform;
gotoAndStop(9);
break;
case 9:
fill_9.transform.colorTransform = myColorTransform;
gotoAndStop(10);
break;
case 10:
fill_10.transform.colorTransform = myColorTransform;
gotoAndStop(11);
break;
case 11:
fill_11.transform.colorTransform = myColorTransform;
gotoAndStop(12);
break;
case 12:
fill_12.transform.colorTransform = myColorTransform;
gotoAndStop(13);
break;
case 13:
fill_13.transform.colorTransform = myColorTransform;
gotoAndStop(14);
break;
case 14:
fill_14.transform.colorTransform = myColorTransform;
gotoAndStop(15);
break;
case 15:
fill_151.transform.colorTransform = myColorTransform;
gotoAndStop(16);
break;
case 16:
gotoAndStop(17);
break;
}
});
btn_next_arrow.addEventListener(MouseEvent.MOUSE_OVER, function hovering(e:MouseEvent):void {
trans.color = uint(0x606266);
next_arrow.transform.colorTransform = trans;
});
btn_back_arrow.addEventListener(MouseEvent.MOUSE_OVER, function hovering1(e:MouseEvent):void {
trans1.color = uint(0x606266);
back_arrow.transform.colorTransform = trans1;
});
btn_next_arrow.addEventListener (MouseEvent.MOUSE_OUT, function hideShadow(event:MouseEvent):void {
trans.color = uint(0x999999);
next_arrow.transform.colorTransform = trans;
});
btn_back_arrow.addEventListener (MouseEvent.MOUSE_OUT, function hideShadow1(event:MouseEvent):void {
trans1.color = uint(0x999999);
back_arrow.transform.colorTransform = trans1;
});
btn_back_arrow.addEventListener(MouseEvent.CLICK, function goPrevLectFrame(e:MouseEvent):void {
switch(currentFrame){
case 2:
gotoAndStop(1);
break;
case 3:
gotoAndStop(2);
break;
case 4:
gotoAndStop(3);
break;
case 5:
gotoAndStop(4);
break;
case 6:
gotoAndStop(5);
break;
case 7:
gotoAndStop(6);
break;
case 8:
gotoAndStop(7);
break;
case 9:
gotoAndStop(8);
break;
case 10:
gotoAndStop(9);
break;
case 11:
gotoAndStop(10);
break;
case 12:
gotoAndStop(11);
break;
case 13:
gotoAndStop(12);
break;
case 14:
gotoAndStop(13);
break;
case 15:
gotoAndStop(14);
break;
case 16:
gotoAndStop(15);
break;
}
});
btn_main_menu.addEventListener(MouseEvent.CLICK, function goPrevLectFrame1(e:MouseEvent):void {
gotoAndStop(17);
});
The problem is when I get to the last frame and click the next button, it says:
TypeError: Error #1009: ... "null". at
Function/()[RECOVER_shitty_fla.MainTimeline::frame1:114]
That's a problem causing function:
btn_next_arrow.addEventListener (MouseEvent.MOUSE_OUT, function hideShadow(event:MouseEvent):void {
trans.color = uint(0x999999);
next_arrow.transform.colorTransform = trans; // LINE 114
});
I'm really struggling here, will appreciate any help, thank in advance.
You need to remove the event listeners before entering the final frame. Your program is still listening for MouseEvents on objects that do not exist on that frame.
Additionally, I'd heed #BadFeelingAboutThis's advice. Inline functions are bad practice and should generally be avoided.
There are other problems with this code (mostly minor). I've tweaked your code from the FLA document to get the program working. See the changes below.
Frames 1-16
stop();
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.StageScaleMode;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.geom.ColorTransform;
/** initializtios **/
var trans:ColorTransform = next_arrow.transform.colorTransform;
var p:int = 1;
var myColorTransform = new ColorTransform();
myColorTransform.color = 0x42f498;
var trans1:ColorTransform = back_arrow.transform.colorTransform;
/** actual code **/
btn_next_arrow.addEventListener(MouseEvent.CLICK, goNextLectFrame);
function goNextLectFrame(e:MouseEvent):void {
if (p==1) {
fill_1.transform.colorTransform = myColorTransform;
fill_22.alpha = 0.00001;
fill_3.alpha = 0.00001;
fill_33.alpha = 0.00001;
fill_5.alpha = 0.00001;
fill_6.alpha = 0.00001;
fill_7.alpha = 0.00001;
fill_8.alpha = 0.00001;
fill_9.alpha = 0.00001;
fill_10.alpha = 0.00001;
fill_11.alpha = 0.00001;
fill_12.alpha = 0.00001;
fill_13.alpha = 0.00001;
fill_14.alpha = 0.00001;
fill_151.alpha = 0.00001;
++p;
}
switch (currentFrame) {
case 1:
gotoAndStop(2);
break;
case 2:
fill_22.transform.colorTransform = myColorTransform;
gotoAndStop(3);
break;
case 3:
fill_3.transform.colorTransform = myColorTransform;
gotoAndStop(4);
break;
case 4:
fill_33.transform.colorTransform = myColorTransform;
gotoAndStop(5);
break;
case 5:
fill_5.transform.colorTransform = myColorTransform;
gotoAndStop(6);
break;
case 6:
fill_6.transform.colorTransform = myColorTransform;
gotoAndStop(7);
break;
case 7:
fill_7.transform.colorTransform = myColorTransform;
gotoAndStop(8);
break;
case 8:
fill_8.transform.colorTransform = myColorTransform;
gotoAndStop(9);
break;
case 9:
fill_9.transform.colorTransform = myColorTransform;
gotoAndStop(10);
break;
case 10:
fill_10.transform.colorTransform = myColorTransform;
gotoAndStop(11);
break;
case 11:
fill_11.transform.colorTransform = myColorTransform;
gotoAndStop(12);
break;
case 12:
fill_12.transform.colorTransform = myColorTransform;
gotoAndStop(13);
break;
case 13:
fill_13.transform.colorTransform = myColorTransform;
gotoAndStop(14);
break;
case 14:
fill_14.transform.colorTransform = myColorTransform;
gotoAndStop(15);
break;
case 15:
fill_151.transform.colorTransform = myColorTransform;
btn_next_arrow.removeEventListener(MouseEvent.MOUSE_OVER, hovering);
btn_next_arrow.removeEventListener(MouseEvent.MOUSE_OUT, hideShadow);
btn_back_arrow.removeEventListener(MouseEvent.MOUSE_OVER, hovering1);
btn_back_arrow.removeEventListener(MouseEvent.MOUSE_OUT, hideShadow1);
gotoAndStop(16);
break;
case 16:
gotoAndStop(17);
break;
}
}
btn_next_arrow.addEventListener(MouseEvent.MOUSE_OVER, hovering);
function hovering(e:MouseEvent):void {
trans.color = uint(0x606266);
next_arrow.transform.colorTransform = trans;
}
btn_back_arrow.addEventListener(MouseEvent.MOUSE_OVER, hovering1);
function hovering1(e:MouseEvent):void {
trans1.color = uint(0x606266);
back_arrow.transform.colorTransform = trans1;
}
btn_next_arrow.addEventListener (MouseEvent.MOUSE_OUT, hideShadow);
function hideShadow(event:MouseEvent):void {
trans.color = uint(0x999999);
next_arrow.transform.colorTransform = trans;
}
btn_back_arrow.addEventListener (MouseEvent.MOUSE_OUT, hideShadow1);
function hideShadow1(event:MouseEvent):void {
trans1.color = uint(0x999999);
back_arrow.transform.colorTransform = trans1;
}
btn_back_arrow.addEventListener(MouseEvent.CLICK, goPrevLectFrame);
function goPrevLectFrame(e:MouseEvent):void {
switch(currentFrame){
case 2:
gotoAndStop(1);
break;
case 3:
gotoAndStop(2);
break;
case 4:
gotoAndStop(3);
break;
case 5:
gotoAndStop(4);
break;
case 6:
gotoAndStop(5);
break;
case 7:
gotoAndStop(6);
break;
case 8:
gotoAndStop(7);
break;
case 9:
gotoAndStop(8);
break;
case 10:
gotoAndStop(9);
break;
case 11:
gotoAndStop(10);
break;
case 12:
gotoAndStop(11);
break;
case 13:
gotoAndStop(12);
break;
case 14:
gotoAndStop(13);
break;
case 15:
gotoAndStop(14);
break;
case 16:
gotoAndStop(15);
break;
}
}
btn_main_menu.addEventListener(MouseEvent.CLICK, goPrevLectFrame1);
function goPrevLectFrame1(e:MouseEvent):void {
gotoAndStop(17);
}
Frame 17
stop();
import flash.events.MouseEvent;
btn_goto_lect1.alpha = 0.00001;
btn_goto_pract.alpha = 0.00001;
btn_goto_lect1.addEventListener (MouseEvent.MOUSE_OVER, showShadow);
function showShadow(event:MouseEvent):void {
btn_goto_lect1.alpha = 1;
}
btn_goto_lect1.addEventListener (MouseEvent.MOUSE_OUT, hideShadow_);
function hideShadow_(event:MouseEvent):void {
btn_goto_lect1.alpha = 0.00001;
}
btn_goto_pract.addEventListener (MouseEvent.MOUSE_OVER, showShadow1);
function showShadow1(event:MouseEvent):void {
btn_goto_pract.alpha = 1;
}
btn_goto_pract.addEventListener (MouseEvent.MOUSE_OUT, hideShadow2);
function hideShadow2(event:MouseEvent):void {
btn_goto_pract.alpha = 0.00001;
}
btn_goto_lect1.addEventListener (MouseEvent.CLICK, showShadow3);
function showShadow3(event:MouseEvent):void {
btn_goto_lect1.removeEventListener (MouseEvent.MOUSE_OVER, showShadow);
btn_goto_lect1.removeEventListener (MouseEvent.MOUSE_OUT, hideShadow_);
btn_goto_pract.removeEventListener (MouseEvent.MOUSE_OVER, showShadow1);
btn_goto_pract.removeEventListener (MouseEvent.MOUSE_OUT, hideShadow2);
gotoAndStop(1);
}

Actionscript 3-- Trying to fix a delay that is occuring upon key press.

I'm trying to setup keyboard control for my game and ran into an interesting obstacle: When the player presses a key to move in a specific direction a delay in the physical movement occurs similar to the delay that happens when editing text.
For instance, When you hold down the "a" key (just as an example, of course it could be any key) and there is a second delay before the cursor will then register "aaaaaaa". The same problem is happening here, so when a direction key is pressed the frame animations begin before the physical movement starts. Which results in an animation that looks like the character is running in place and then finally starts moving after about a 1 or 2 seconds.
Any thoughts, ideas, or advice on a fix would be much appreciated. Thanks in advance, all.
import flash.events.Event;
import flash.events.KeyboardEvent;
character.stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
stage.addEventListener(Event.ENTER_FRAME, onEnterThisFrame);
var moving:int = 4;
var animate:Boolean = false;
function onKeyPress(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case 37: moving = 1; character.gotoAndStop(6); character.x-=5; break; //left
case 38: moving = 2; character.gotoAndStop(4); character.y-=5; break; //up
case 39: moving = 3; character.gotoAndStop(8); character.x+=5; break; //right
case 40: moving = 4; character.gotoAndStop(2); character.y+=5; break; //down
case 32: handleAttack();
}
animate = false;
}
function onKeyRelease(e:KeyboardEvent):void
{
switch(moving)
{
case 1: character.gotoAndStop(6); break; //left
case 2: character.gotoAndStop(4); break; //up
case 3: character.gotoAndStop(8); break; //right
case 4: character.gotoAndStop(2); break; //down
}
animate = true;
}
function handleAttack():void
{
switch (moving)
{
case 1: character.gotoAndStop(11); break; //left
case 2: character.gotoAndStop(10); break; //up
case 3: character.gotoAndStop(12); break; //right
case 4: character.gotoAndStop(9); break; //down
}
}
function onEnterThisFrame(e:Event):void
{
if (animate == true)
{
switch (moving)
{
case 1: if(character.currentFrame == 6) character.gotoAndStop(5); break;
case 2: if(character.currentFrame == 4) character.gotoAndStop(3); break;
case 3: if(character.currentFrame == 8) character.gotoAndStop(7); break;
case 4: if(character.currentFrame == 2) character.gotoAndStop(1); break;
}
}
}
Have bools for each direction and change your onKeyPress and onKeyRelease to set these, then check them and move your character in your onEnterThisFrame function.

AS3 Collision Detection through hitTestPoint for a map based game

I am trying to work out the best way to perform a hitTestPoint for a game with a character, enemies and objects that are non-walkable in the map.
I have created a version that works, which can be seen here:
http://www.hosted101.net/hit/mapHitTest.html (arrow keys to move, you're the square).
Using a previously suggested method, I have created a secondary version of all objects on the map, increased by half the size of the player and am running a hitTestPoint from centerX and centerY of the player.
This is great and works fine.
However, I feel it is not optimal and it could be unnessesary to have all these extra "boundry boxes" created, even if the user can't see them.
So, I created a second version. This time without the boundry boxes:
http://www.hosted101.net/hit/mapHitTestNew.html (arrow keys to move).
As this is still running from centerX and centerY of the player, if you approach an object but are below centerX/Y, you will be able to travel through it.
My question is this: to hitTest objects vs players/enemies/anything on a game like this, what is the best method? (with a fixed example for this simple project would be great)
Is it really creating a duplication of the map layer with additional bounding boxes, or can it be done in a more clean way?
My code is below, for mapHitTestNew.
Thanks.
var moveL:Boolean = new Boolean
var moveR:Boolean = new Boolean
var moveU:Boolean = new Boolean
var moveD:Boolean = new Boolean
var moveSpeed:int = 2
stage.addEventListener(KeyboardEvent.KEY_DOWN, updateKeysDown)
stage.addEventListener(KeyboardEvent.KEY_UP, updateKeysUp)
function updateKeysDown(e:KeyboardEvent){
var _keyCode = e.keyCode
switch (_keyCode){
case 37:
moveL = true;
break;
case 38:
moveU = true;
break;
case 39:
moveR = true;
break;
case 40:
moveD = true;
break;
}
collisionCheck()
}
function updateKeysUp(e:KeyboardEvent){
var _keyCode = e.keyCode
switch (_keyCode){
case 37:
moveL = false;
break;
case 38:
moveU = false;
break;
case 39:
moveR = false;
break;
case 40:
moveD = false;
break;
}
}
/**
***
*** Hit Test
***
***
**/
function collisionCheck():void{
var _MC:MovieClip = new MovieClip;
_MC.x = player.x
_MC.y = player.y
_MC.mouseEnabled = false
var centerX:Number = _MC.x + (player.width/2)
var centerY:Number = _MC.y + (player.height/2)
if(moveL){
trace("Stage 1 Move")
centerX -= moveSpeed
if(map.hitTestPoint((centerX-player.width/2), centerY, true)){
trace("Stage 1 HIT TEST!")
moveL = false
}
}
if(moveR){
trace("Stage 2 Move")
centerX += moveSpeed
if(map.hitTestPoint((centerX+player.width/2), centerY, true)){
trace("Stage 2 HIT TEST!")
moveR = false
}
}
if(moveU){
trace("Stage 3 Move")
centerY -= moveSpeed
if(map.hitTestPoint(centerX, (centerY-player.height/2), true)){
trace("Stage 3 HIT TEST!")
moveU = false
}
}
if(moveD){
trace("Stage 4 Move")
centerY += moveSpeed
if(map.hitTestPoint(centerX, (centerY+player.height/2), true)){
trace("Stage 4 HIT TEST!")
moveD = false
}
}
refreshMovement()
}
function refreshMovement():void{
if(moveL){
player.x -= moveSpeed
}
if(moveR){
player.x += moveSpeed
}
if(moveU){
player.y -= moveSpeed
}
if(moveD){
player.y += moveSpeed
}
}
It sounds like you're doing a kind of over-head view (considering you have the box moving in X and Y). I would have an 2D array with 0's and 1's in it (true/falses). When your character moves around, look up your characters hit box for the tiles that it occupies, which will generally be 1 to 4 tiles.
If there is a TRUE in any of thoses spots, he is hitting something. This way you only need hit boxes on objects that are active (they move etc)
I would also check out this article, and implement the solution that seems to work best for you:
http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

Camera following object

I am fairly new to coding. I am was wondering if there was a way so when I move my object with keys I can get the camera to keep on my object so I can move around a larger world than just my viewport.
I have tried to move the world around instead of my object but for what I am doing it makes my coding a lot more difficult
Here is the code I have.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown );
function keydown (event:KeyboardEvent):void{
switch (event.keyCode){
case Keyboard.LEFT :
ball.x -= 10;
break ;
case Keyboard.RIGHT :
ball.x += 10;
break;
case Keyboard.UP :
ball.y -= 10;
break;
case Keyboard.DOWN :
ball.y += 10;
break;
default :
break;
}
}
you could have the world and the character in a holding sprite/movieclip
then as you move the character, you can set the position of the holder in the opposite direction.
you can use global to local to get the position of the ball, and then can even apply some movement smoothing
here's some code that should work easy enough
you need a movieclip called holder inside that is ball , the holder would also have your world and world assets. And as long as there's no scaling you don't need to use globalToLocal
var middlePt:Point = new Point(stage.stageWidth/2, stage.stageHeight/2);
var pt:Point = new Point(holder.ball.x, holder.ball.y);
var destPoint:Point = new Point(-pt.x + middlePt.x, -pt.y + middlePt.y);
addEventListener(Event.ENTER_FRAME,enterFrame);
function enterFrame(e:Event):void{
holder.x = holder.x*0.5 + destPoint.x*0.5;
holder.y = holder.y*0.5 + destPoint.y*0.5;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown );
function keydown (event:KeyboardEvent):void{
switch (event.keyCode){
case Keyboard.LEFT :
holder.ball.x -= 10;
break ;
case Keyboard.RIGHT :
holder.ball.x += 10;
break;
case Keyboard.UP :
holder.ball.y -= 10;
break;
case Keyboard.DOWN :
holder.ball.y += 10;
break;
default :
break;
}
middlePt = new Point(stage.stageWidth/2, stage.stageHeight/2);
pt = new Point(holder.ball.x, holder.ball.y);
destPoint = new Point(-pt.x + middlePt.x, -pt.y + middlePt.y);
}