I am looking for direction to this old UFC effect - http://84.ufc.com/ that appears on the main page. It is movieclips rotating on different axis based on the mouse position. So far I have found this script:
stage.addEventListener(MouseEvent.MOUSE_MOVE,EnterFrame);
function EnterFrame(e:Event)
{
mc.rotation = (180*Math.atan2(mouseY-mc.y,mouseX-mc.x))/Math.PI + 90;
}
But this only rotates on x and y. What's a way to approach this effect? Please any suggestions. I have searched this for months.
If you are using Flash CS4+ and targeting Flash Player 10+, you can use the 3D DisplayObject APIs (aka "postcards in space") to achieve this effect! All DisplayObjects will have x, y, z, rotationX, rotationY, and rotationZ properties that you can tweak.
Create a movieclip and place it on the stage. The origin--the crosshair that appears when the clip is selected--should be in the middle in the stage. Give the movieclip an instance name of clip.
Double-click the movieclip, and place other movieclips inside it. Use the 3D Rotation and Translation tools to orient these clips in 3D inside your parent clip. You can find the 3D tools in your toolbar -- it has an egg-like icon, or press the W or G keys on your keyboard.
Now, here's some simple code that will tweak the orientation of that parent clip based on the mouse position:
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
clip.rotationX = (stage.mouseY - stage.stageHeight/2) * 0.1;
clip.rotationY = (stage.mouseX - stage.stageWidth/2) * 0.1;
}
You can play around with this to come up with many other effects. Note that you can only do simple 3D effects with these properties, however. You can't do full 3D rotation, because the clips won't be sorted from back to front. For more complex effects, you'll want to use a framework like Papervision3D or Five3D.
i just found out...
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
anim.rotationX += ((stage.mouseY - stage.stageHeight/2)-anim.rotationX*3) * 0.05;
anim.rotationY += ((stage.mouseX - stage.stageWidth/2)-anim.rotationY*5) * 0.05;
}
Related
I'm working in HTML5 canvas + Easeljs
I would like to rotate objects in my application using my mouse. Idea is the following: click on an object > circle appears with draggable box > drag this box over the circle makes the object rotate.
It is kinda the same question as this one, but I have no clue how to do this in Easeljs.
To illustrate
Now you can rotate objects by clicking the buttons in the top right (L = left, R = right), but I want it to be like here:
Click + drag on white box will make it rotate
Help is much appreciated!
You have to add listeners to to the stage (or in your case, maybe the playing field Shape) for the mousedown, mousemove, and mouseup. In this example, I'm just listening to the global stagemouseup, stagemousedown, and stagemousemove events. I don't assign the stagemousemove listener until the stagemousedown is fired. I then remove it when the stagemouseup is fired.
For each stagemousemove event, you need to find the angle between your player and the stage mouse position. You can do this will the following formula where shape is your player:
var angleInRadians = Math.atan2(stage.mouseY - shape.y, stage.mouseX - shape.x);
var angleInDegrees = angleInRadians * (180 / Math.PI);
Then you simply set the shape.rotation = angleInDegrees.
Check out this working example.
I am creating a game like bounce ball using cocos2d-x and box2d. I have different objects like rectangle, square etc.. I am able to detect collision, but i want to detect collision only on top of the objects. What exactly i want is, when the ball is on the top of the object, then only i want to jump the ball.
But, when the ball is collide on remaining side(bottom or left or right) i don't want to jump the ball.
In touchbegan, i am using the following code to the bounce ball. So every touch it is jumping when it collide with remaining side.
if(_ball->boundingBox().intersectsRect(rect->boundingBox()))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
Any advice?
Following steps can solve your problem-
1) CCRect projectileRect = CCRect(float x, float y, float width, float height);
if(_ball->boundingBox().intersectsRect(projectileRect))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
2) - Make body of an object and then check their collision.
I got the solution for my question from the below link.
http://www.raywenderlich.com/28606/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-2
I have a custom cursor in my flash project. That cursor consists of several parts (i.e. several movie clips inside of the cursor movie clip). And when the cursor rolls over different movie clips in my project, the parts of the cursor distort a little bit as though moving 1 pixel in relation to each other. And therefore the look of the whole cursor distorts a little. This happens every time the cursor crosses the boundary between the movie clips of the project (buttons, design pieces and so on). How can I make my cursor always retain one and the same look?
Thanks in advance
I'm guessing you're doing something like this to position your custom cursor:
stage.addEventListener(MouseEvent.MOUSE_MOVE, moved);
function moved(e:MouseEvent):void {
customCursor.x = e.stageX;
customCursor.y = e.stageY;
}
If so, when you move the mouse over a MovieClip or other element, your listener is receiving the event from that DisplayObject rather than the Stage. For some reason, DisplayObjects positioned at sub pixel values produce e.stageX and e.stageY values which aren't exactly the same as stage.mouseX and stage.mouseY, so your custom cursor elements are jumping slightly as the pixel values round differently.
Try using the Stage mouse position directly instead:
stage.addEventListener(MouseEvent.MOUSE_MOVE, moved);
function moved(e:MouseEvent):void {
customCursor.x = stage.mouseX;
customCursor.y = stage.mouseY;
}
I'm writing a paint program that uses shape brushes to draw by using the matrix function.
Everything works well aside from the fact that it's not smooth at all. There will be gaps in the painting if the mouse is moved at a high speed.
I've looked everywhere but haven't been able to find any solution.
The code basically looks like this:
//Press mouse within container. Uses Matrix to draw instances of the brush.
private function handleMouseDown_drawContainer(e:MouseEvent):void
{
_matrix.identity();
_matrix.translate(mouseX - 10, mouseY - 30);
_layout.bitmapData.draw(_layout.brush, _matrix);
_layout.drawContainer.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove_drawContainer);
_layout.drawContainer.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp_drawContainer)
}
//Move mouse within container. Uses Matrix to draw instances of the brush.
private function handleMouseMove_drawContainer(e:MouseEvent):void
{
_matrix.identity();
_matrix.translate(mouseX - 10, mouseY - 30);
_layout.bitmapData.draw(_layout.brush, _matrix);
}
If anyone could help me figure out how to smooth out the drawing, I'd be forever grateful! =p
Thanks in advance.
You probably need some kind of interpolation between the mouse positions... there are of course many ways, I'll describe one very easy to implement but a bit hard to fine tune. Basically instead of drawing in each mouse position, you use an easing equation that follows the mouse with some delay... this way the described line will be a bit smoother, and will draw a few times between each mouse position.
So instead of doing (pseudocode):
onMouseMove {
draw(mouseX, mouseY);
}
You do something like:
x = 0;
y = 0;
onEnterFrame {
x += (mouseX - x) * 0.2;
y += (mouseY - y) * 0.2;
draw(x, y);
}
Although maybe what you really need is a way to limit the maximum distance between points, so if the mouse moves more in one frame, you interpolate points between the two positions and draw as many times as it's needed.
Or if you're looking for smoother lines (avoid sharp corners) maybe you also need to use beziers to control the resulting line.
Anyway, it all depends on the kind of drawing you're looking for.
To anybody who may care to help:
I am looking to create an animation (perhaps frame by frame) that corresponds with the coordinates of an object. Specifically, I want to have a draggable object's coordinates (locked to the x-axis) indicate where the playhead of a specific movie clip should be.
In other words, let's say that I have a 100px wide stage and I want each px location of an object on that stage to correspond to a particular frame of a movieclip.
In concept, I feel that it should be as easy as loading an objects coordinates into a variable, then passing that variable on with a simple math equation, adjusting it for movieclip length... but right about then my brain gets fried.
Finding out how to lock a draggable object to the x-axis has been pretty easy, but from there I'm stumped. I'm not particularly well versed in AS3 but I do like to think I understand the concepts.
Thank you in advance.
Try the following:
import flash.events.Event;
//the min (left-most) coord your draggable mc can be dragged
var minX:int=0;
//the max (right-most) coord your draggable mc can be dragged
var maxX:int=100;
var frameTo:uint;
//enterframe listener to check drag_mc x position continuously
addEventListener(Event.ENTER_FRAME, enterframe_handler);
function enterframe_handler(e:Event):void
{
//drag_mc is your draggable movieclip, anim_mc is the animation
//drag_mc.x should always be between minX and maxX: (minX <= drag_mc.x <= maxX)
//(drag_mc.x/(maxX - minX) gives us the "percentage" (from 0 to 1)
//multiply by the animation's total frames lenght,
// and add 1 (because frame numbers begin at 1)
frameTo = 1 + Math.floor((drag_mc.x/(maxX - minX))* anim_mc.totalFrames);
//set animation to target frame!
anim_mc.gotoAndStop(frameTo);
}