as3 - tweenlite X coordinate approaches and gets slower - actionscript-3

I have a rookie problem with Tweenlite. As I am playing around with it, I noticed when my animation approaches closer to its coordinate "x:100 It begins to gradually slow down. How do I make it so that from the time the object begins moving, to the very end, the speeds stays the same. Also when the object first begins movement, it starts out on a regular speed, and gradually becomes slower as its making its way.
TweenLite.to(my_box.anotherBoxInside, 5, {x:100, ease:Linear.easeNone,
onStart:myFunction, onComplete:myFunctionn});

Change ease parameter to: Power0.easeNone
TweenLite.to(my_box.anotherBoxInside, 5, {x:100, ease:Power0.easeNone, onStart:myFunction, onComplete:myFunctionn});

Related

AS3/Air drawing slows down over time

I have an Air app written in AS3 using FlashDevelop that involves using the graphics.drawEllipse method to draw 96 ellipses on the stage each frame (20fps), updating their position and alpha. After a second or so they fade out and are removed.
graphics.clear();
graphics.beginFill( color, timer );
graphics.lineStyle( 0, color, timer );
graphics.drawEllipse( x, y, r, r );
graphics.endFill();
The first 7 or so times these groups of 96 ellipses are spawned, they flow very smoothly across the screen as intended. After that they start to stutter like it's a framerate issue.
They are the only moving part in the scene and so are the only visible sign the system is struggling to draw them, but I am aware they may be the symptom rather than the cause of the slowdown. I have used the profiler and believe I have eliminated all the memory leaks.
I am wondering if anyone can see a problem with what I'm doing, or if not has any other suggestions with what could be causing the slowdown (I guess just general as3 things that could cause it).

Restrict max speed in tweenmax

I'm developing game in Flash and I need to move my movie-clips in a wrap-round way across the screen.
For this I m using Tweenmax.to() function provided by greensock.
The function takes 'time' and 'distance' as parameters and applies accleration and deaccleration for the motion itself.
However this takes max-speed of the motion beyond what I want.
Is there a way I could set the max-speed for the motion as well?
I do not think you can control the acceleration manually, but to slow speed down you can either set higher value of time:
say, from
TweenMax.to(mc, 2, {x:65, y:117});
to
TweenMax.to(mc, 4, {x:65, y:117});
Or use another type of easing - every of them contains its own set of acceleration.
There are a lot of them, you could try to find appropriate one.
There is an interactive demo, try playing with different easing functions.

Moving the "camera" of an HTML Canvas element

I'm trying to find a clean way to "move the camera" of a canvas element.
This for my prototype game (side scroller). I'd love to think there's a better solution than moving the whole set of nodes to simulate a "camera" moving around.
Am almost certain to have read a simple how-to (using offsets?) but the fact I don't find anything like that starts to raise doubts... have I imagined reading that!?
Thanks to help me clarify...
J
Presumably you redraw your whole game scene 30 times a second (more or less)
You need to redraw your whole game scene but first translate the Canvas context by some offset.
context.translate(x,y) is precisely what you want. You'll want to read up on the use of that as well as the save() and restore() methods.
When you translate the context, everything drawn afterwards is shifted by that amount.
So you constantly draw something (maybe an enemy) at 50,50 using drawImage(badguy,50,50). Then the player moves, which changes the x of translate to -1 (because the player is moving to the right) instead of 0. You still draw the enemy sprite with the command drawImage(badguy,50,50), but when you draw it the enemy shows up as if it were at 49,50 because of the context.translate(-1,0) command shifting everything before its drawn.
Of course when you get into performance you'll want to be making sure that you are only ever drawing things that can actually be seen on the screen! If your are far down the level with context.translate(-2000,0), you dont want to be drawing objects at 50,50 anymore, only ones that intersect the viewable area.

Blur show effect - html5 canvas

I want to create a blur show effect like : http://www.flasheff.com/patternsshowcase/ (FESBlur).
I've tried to use http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html using setInterval and changing the radius of the effect on each "frame", but if I set the interval delay to a lower value(increase the effect's speed), it doesn't runs smooth ( I think it's beacuse it uses imageData, and changes the value of every pixel).
Do you have any ideea about how I could make that effect to run fast enough? (about 1 second from max blur to non-blur)
Thanks,
Gabriel
I'm no expert at any of this, but some very obvious things come to mind:
Method 1: Buffering. Buffering is probably the single most effective way to stop stutters. If you could delay the initiation of the animation by .25 seconds before actually outputting it, you could probably get half of the calculations done before the animation even starts.
Method 2: Caching. The time that a blur takes to process usually grows as the radius of the blur grows. Let's say you have 20 frames in your animation. If you can cache frame 5, 10, 15, and 20 (unblurred image) then you can get frames 1-4 from blurring 5 with a small radius, and you'll get frame 5 for free, then you can get frames 6-9 from blurring 10 with a small radius, and you'll get frame 10 for free, and so on.
Method 3: Interpolation/Blending of Coarse Blurs. Blending two images should be quicker than blurring one image. If you make your animation 21 frames (1-21), then you should be able to calculate frames 1, 5, 9, 13, 17, and 21. Those frames would be free, and you'd the other frames by blending them: frame 2 (F2) would be 75% F1 and 25% F5, F3 would be 50% F1 and 50% F5, and so on.
My guess is that this would take a decent amount of tinkering on your part. None of these are going to fix your problem by simply changing a parameter or two to some magic numbers.
Realize this, however: javascript doesn't really get to pick and choose when it will or will not get the attention of the processor, and it's not exactly high on the processor's list of priorities. If the processor decides to go on vacation for 1/10th of a second, there's probably nothing that you can do to stop the stutter.
Use requestAnimFrame instead of setInterval. More info on http://paulirish.com/2011/requestanimationframe-for-smart-animating/
I tested this with faster blur reduction on my FF5 and it seems to perform fine.
(function animloop(){
blurImage();
requestAnimFrame(animloop);
})();
function blurImage(){
stackBlurImage( "image1", "output", amount, true );
amount=amount-4;
}

Actionscript collisions: solving exceptions and strange cases

I have created a collision class to detect collisions using pixels. In my class I've also developed some functions for determining the collsion angle. Based on this I created some examples:
http://megaswf.com/serve/25437/
http://megaswf.com/serve/25436/
(Space to change gravity, right/left to give some speed to the ball.)
As you will probably notice, there are strange things that happen:
When the ball speed is very low
When the direction of the ball is
almost tangent to the obstacle.
collision http://img514.imageshack.us/img514/4059/colisao.png
The above image shows how I calculate the collision angle.
I call the red dots the keypoints. I search for a maximum number of keypoints (normally 4). If I find more then 2 keypoints, I choose the 2 farthest ones (as shown in one of the blue objects). Thats how I then find the normal angle to where in the surface the object collided. I don't know if this is an obsolete way of doing things.
based on that angle, I rotate the speed vector to do the bouncing.
The piece of code to do the maths is here:
static public function newSpeedVector(speedX: Number, speedY: Number, normalAngle: Number): Object{
var vector_angle: Number = Math.atan2(speedY, speedX) * (180/Math.PI);
var rotating_angle: Number = (2*(normalAngle - vector_angle) + 180) % 360;
var cos_ang: Number = Math.cos(rotating_angle/DEGREES_OF_1RAD);
var sin_ang: Number = Math.sin(rotating_angle/DEGREES_OF_1RAD);
var final_speedX: Number = speedX * cos_ang - speedY * sin_ang;
var final_speedY: Number = speedX * sin_ang + speedY * cos_ang;
return {x_speed: final_speedX, y_speed: final_speedY};
}
This is how the new speed vector is calculated...
My question is, has anyone faced this kind of problem or has some idea on how to avoid this from happening?
Without seeing your code, this is the best I can provide.
Collision physics should have some velocity threshold that is considered "stopped". That is, once the velocity gets small enough you should explicitly mark an object as stopped and exempt it from your collision code. This will help stabilize slow moving objects. Trial and error is required for a good threshold.
When a collision happens, it is important to at least attempt to correct any inter-penetration. This is most likely the reason why tangent collisions are behaving strangely. Attempt to move the colliding object away from what it hit in a reasonable manner.
Your method of determining the normal should work fine. Game physics is all about cheating (cheating in a way that still looks good). I take it rotation and friction isn't a part of what you're going for?
Try this. You have the contact normal. When you detect a collision, calculate the penetration depth and move your object along the normal so that is isn't penetrating anymore. You can use the two points you have already calculated to get one point of penetration, you'll need to calculate the other though. For circles it's easy (center point + radius in direction of normal). There are more complex ways of going about this but see if the simple method works well for you.
I have read and recommend this book: Game Physics Engine Development
I did something a little like that and got a similar problem.
What I did is that when the pixels from the bouncing object (a ball in your case) overlap with the pixels from the obstacle, you need to move the ball out of the obstacle so that they are not overlapping.
Say the ball is rolling on the obstacle, before your render the ball again you need to move it back by the amount of 'overlap'. If you know at what angle the ball hit the obstable just move it out along that angle.
You also need to add some damping otherwise the ball will never stop. Take a (very) small value, if the ball velocity is bellow that value, set the velocity to 0.
You can try one more thing, which I think is very effective.
You can make functions such as getNextX() and getNextY()(Which of course give their position coordinated after the next update) in your game objects and Check collision on objects based on their next position instead of their current position.
This way, the objects will never overlap, you'll know when they are about collide and apply your after collision physics gracefully!