Is this possible to lock the mouse along a path and if so how? - actionscript-3

I want to be able to lock a mouse to move along an arc only. Is this possible? And if so how? I have tried looking on Google and forums. All I have found are locking movieclips to a path. I am using ActionScript 3 in Adobe Flash CS6.

hide the mouse cursor
create a custom cursor (like actual cursor)
change position of your cursor (use mouse position)
this code will give you an idea
Mouse.hide();
var path:Array=[new Point(0,0),new Point(50,20),new Point(150,40),new Point(250,60),new Point(300,70),new Point(400,80)];
var myCursor:Sprite=new Sprite();
myCursor.graphics.beginFill(0);
myCursor.graphics.drawCircle(0,0,5);
myCursor.graphics.endFill();
addChild(myCursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE,refreshCursorPosition);
function refreshCursorPosition(e:MouseEvent):void{
if(e.stageX<0 || e.stageX>stage.stageWidth)
return;
var pos:int=Math.floor(e.stageX*path.length/stage.stageWidth);
myCursor.x=path[pos].x;
myCursor.y=path[pos].y;
}

This documentation might be what you're looking for.
http://evolve.reintroducing.com/2011/05/09/as3/as3-drag-a-clip-along-an-arc/
Take a look at the example first.

Related

Flash Actionscript 3.0 Mouse Cursor Duplicates

I wrote a simple game and I want to add custom mouse cursor. I created MovieClip called Pointer, exported it to AS3 and wrote this code:
/* Custom Mouse Cursor
Replaces the default mouse cursor with the specified symbol instance.
*/
stage.addChild(movieClip_2);
movieClip_2.mouseEnabled = false;
movieClip_2.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_3);
function fl_CustomMouseCursor_3(event:Event)
{
movieClip_2.x = stage.mouseX;
movieClip_2.y = stage.mouseY;
}
Mouse.hide();
//To restore the default mouse pointer, uncomment the following lines:
//movieClip_2.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_3);
//stage.removeChild(movieClip_2);
//Mouse.show();
Here is a screenshot:
Whenever I play the game (ctrl enter) it stops the play and duplicates the custom cursor. Is there anyway I can make it not duplicate this is very annoying and I have no idea on how to fix it.
~ EDIT 2 ~
Okay I changed the code to but the problem is now it's showing me the regular cursor and the custom one at the same time.
movieClip_1.mouseEnabled = false; movieClip_1.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor); function fl_CustomMouseCursor(event:Event) { movieClip_1.x = stage.mouseX; movieClip_1.y = stage.mouseY; } stage.removeChild(movieClip_1) Mouse.hide()
~ EDIT 3 ~
Thank you #LDMS for helping me. I had to remove the first line stage.addChild(movieClip_1); and it worked. :)
Most likely, your problem stems from this line:
stage.addChild(movieClip_2);
When you add a movie clip that was created on the timeline to another display object (like the stage), it will not get removed from that new display object except through code.
If your timeline loops, then every loop it will create a new movie clip and add it to the stage (but not remove the old one).
To fix it, do one of the following:
don't loop your timeline (so the code only happens once), eg put a stop() on your timeline
manually remove the movie clip from the stage before the timeline loops (eg stage.removeChild(movieclip_2) at the end of your timeline
Don't add it to the stage to begin with. (just take out the stage.addChild(movieClip_2); line)

Box2d MovieClip to original position

I want to try a simple task where if i move a object inside the world and then press a button it should go back to its original position , but its not working , below is the code i am using - the file is here - http://www.fastswf.com/yAnIvBs (when i remove the event listener)
with event listener - http://www.fastswf.com/rpYsIt8
////////========================
stop();
var startXPos:Number = level1WorldObj.box1.x;
var startYPos:Number = level1WorldObj.box1.y;
function areaS(e:Event) {
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
level1WorldObj.box1.removeEventListener(Event.ENTER_FRAME, areaS);
}
but1.addEventListener(MouseEvent.CLICK,nClick3);
function nClick3(event:MouseEvent):void{
level1WorldObj.box1.addEventListener(Event.ENTER_FRAME, areaS);
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
}
/////////////////======================
Now i want to be able to do it many time so i kept the variables that detect the initial x, y as global ...
Here you can see how it behaves in debugdraw mode , strangely only the clip moves not the actual body - http://www.fastswf.com/-Ijkta4
Can some one please guide me here ...
Thanks in advance ...
Jin
The graphics that you see (box1) aren't related to the physical object behind the scenes - you're currently only moving the graphics not the object itself.
You need to use either SetPosition() or SetTransform() on the b2Body of the object
Edit 07/7
As you're using the Box2D World Construction Kit, I took a look at the source code (available here: https://github.com/jesses/wck). The main class seems to be BodyShape (https://raw.githubusercontent.com/jesses/wck/master/wck/BodyShape.as).
Looking through it, you should be able to access the b2Body directly. If it's null (which is probably the source of the TypeError that you're getting, then you haven't called createBody(), which is what actually takes all of your properties as creates the physical object behind the scenes.
Once you have a b2Body, if you want to position it based on the graphics, there's a function syncTransform() to do just that.
You should turn on debugDraw on your World class to make it easier to see what's going on in the background. NOTE: this needs to be done before calling create()
I was able to find solution to this problem , i found the starting point by using this -
trace(level1WorldObj.box1.b2body.GetPosition().x);
trace(level1WorldObj.box1.b2body.GetPosition().y);
then once i had the position manually i took down the coordinates and used the below code ....
level1WorldObj.box1.b2body.SetTransform(new V2(-2, 2),0 );
Thanks #divillysausages for all the help ...
Regards

FloodFill in AS3

I have been making a basic painting application similar to MS-Paint with basic paint, eraser and fill tools. It's this last one that's giving me some trouble.
I'm pretty new to using BitmapData but the idea is that when the user clicks the board, it triggers the startFloodFill method. This is shown below:
public static function startFloodFill(e:MouseEvent):void
{
trace("FLOODFILL");
var boardRef:MovieClip = e.currentTarget.parent.board; //Creates a reference to the board
var boardData:BitmapData = new BitmapData(boardRef.width, boardRef.height); //Creates a new BitmapData with the same size as boardRef
boardData.floodFill(e.localX, e.localY, 0x00CCCCCC); //Applies the FloodFill
boardData.draw(boardRef); //Saves the boardRef as bitmapData
boardRef.bitmapData = boardData; //Updates the board
boardRef.parent.addChild(boardRef);
}
Can anybody tell me what I've done wrong here? When I click, the board does not change. I expected the FloodFill to fill the entire bitmap with the chosen colour as the board is blank when I click.
I also tried replacing the last two lines with:
boardRef.addChild(new Bitmap(boardData) ); //Updates the board
Thanks
The problem is that you first use the floodFill, and then use the draw method, which actually fills the BitmapData with whatever param you give it - in your case it's the boardRef.
You should first draw the boardRef into the boardData, and then use floodFill. At the end, you need to create new Bitmap and display it.
Now you are setting the bitmapData of a MovieClip?! Don't know what you wanted, but you just need to add new child (new Bitmap)

cocos2dx/coco2d layer transition

I hope to pop down a score panel when players win and pass the gate.
Normally it will pop down score board.
I think the best way is to use layer and pull down it.
But I only get the transition of scene, just wonder is there any way for layer transition?
Did not see an equivalent of CCTransitionScene :CCScene for CCLayer but layers can runActions using which we can bring out most of the animations/transitions.
Here is what I do in such situations but I guess you are thinking of the same thing. Nevertheless,
1.Create a layer and add it as a child at a position outside of your screen frame.
2.Then use CCMoveTo to move it to the desired location when you want to pull it down.
I have done something similar in the past.
Display your layer offscreen
i.e setposition(0, CCDirector::sharedDirector()->getWinSize().height*1.5f);
create an action to move it onscreen (I like to use CCEaseSineOut)
you can also use a callfunc to call a function when it has finished its animation
scoreLayer->runAction( CCSequence::create( CCEaseSineOut::create(CCMoveTo::create(1.0f, ccp(0, 0-_screenHeight*1.5f))), CCCallFunc::create(this, callfunc_selector(MainLayer::scorefinishedMove)), NULL));
Note: that function might need some fixes to ending brackets etc. And you may want to seperate out some of those actions rather than putting the initialization right in the runAction function
For layer transition you can do this:
CCScene* newScene = CCTransitionCrossFade::create(.5f,Layer2::scene());
CCDirector::sharedDirector()->pushScene(newScene);
In Layer2.cpp
CCScene* Layer2::scene()
{
CCScene* scene = CCScene::create();
CCLayer* layer = new Layer2();
scene->addChild(layer,1);
return scene;
}

AS3 Flip effect leaving a trail?

I'm trying to create something with a flip effect tutorial from tutplus - http://active.tutsplus.com/tutorials/effects/iphone-page-transition-flash/
However my flip area is much bigger than the tutorial, it's 900px wide. Everything works fine except that it leaves a trail when the width is that big. You'll see it when you flip it a few times.
Someone else posted the same problem in the comments from last year, but no one replied.
Does anyone know a solution to this?
Edit:
Here is a screen shot: http://imageshack.us/f/823/unled2lo.jpg/ (click to enlarge)
The front is purple and the back is white.
As you can see it left a bit of the purple as the page flipped to white.
I couldn't get a screen shot of it turning, but it's even more obvious as the page is actually flipping because the width become narrow which reveals a whole lot more that's left behind on the page.
The tutorial you are using create the flip effect using the build in flash tween classes, they are absolute rubbish, and very slow if you compare to other third part tween classes. That may be causing the trail! Lee Brimelow has a great video tutorial about how to do exact what you need: http://gotoandlearn.com/play.php?id=91 he is using caurina, but I highly recommend you to replace it with tweenLight so far the best tween I ever used.
Ok, thats the walkthrough:
1- Download the files from Lee Brimelow tutorial here http://gotoandlearn.com/files/3dflip.zip
2- Download tweenLight AS3 classes here: http://www.greensock.com/tweenlite/
3- copy the com folder inside greensock-as3 and past it inside 3dflip folder. Now you have all the tween classes you need for your flip!
4- open the flash file 3dflip.fla and replace the original code (located in the first frame) with this:
import com.greensock.TweenLite;
import fl.video.*;
con.visible = false;
var flv:FLVPlayback = con.vid.flvp;
flv.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, onStart);
function onStart(e:Event):void
{
con.visible = true;
loading.visible = false;
}
con.vid.spin.addEventListener(MouseEvent.CLICK, cl);
con.tclip.spin.addEventListener(MouseEvent.CLICK, cl);
var isTurning:Boolean = false;
function cl(e:Event):void
{
if(!isTurning)
{
TweenLite.to(con, 1, {rotationY:con.rotationY+180, onComplete:function(){isTurning=false;}});
isTurning = true;
}
}
addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void
{
if(con.rotationY > 90 && con.rotationY < 270)
con.addChild(con.tclip);
else
con.addChild(con.vid);
if(con.rotationY >= 360) con.rotationY = 0;
}
Thats it. Now publish and see the result. Now all you have to do is replace the video player with the content that you want!
I would check the state of first side - it seems that it's forgotten on stage when "the other side" kicks in.