If condition explanation AS3 - actionscript-3

I found this code online. But I'm not completely sure what it means. It's to create a bouncing ball. Im just not sure whats saying in this if condition.
is it about the speed of the object or the where it's going to appear in the stage? Could u please add a //comment for a brief explanation. Thank you in advance!
if ( this.x >= nStageWidth - 10 )
{
this.x = nStageWidth - 10;
nSpeedX *= -1;
}
else if ( this.x <= 10 )
{
this.x = 10;
nSpeedX *= -1;
}
if ( this.y >= nStageHeight - 10 )
{
this.y = nStageHeight - 10;
nSpeedY *= -1;
}
else if ( this.y <= 10 )
{
this.y = 10;
nSpeedY *= -1;
}

This code checks the x or y property of an object to make sure it's within certain boundaries. If it's not, the object's nSpeedX or nSpeedY property is multiplied by -1.
For example, if x is less than 10 or greater than or equal to
nStageWidth-10, nSpeedX is multiplied by -1, which I assume sends
the object traveling in the opposite direction.
Without more code, I can't give you the exact implementation. However, based on how everything's named, my guess is that this code sends an object bouncing from one side of the stage to the other (with 10 pixels of padding on either side).

Related

Mario pass through the top of a platform even if the collision was detected (flash CS6)

i am trying to develop a plataform game in flash cs6. I created a class called Plataforma in which a method called enterFrameEvents is responsable for verifying if a platform of such class has collided with mario.
However, no matter what I do, it detects that mario has collided with its top, but mario pass through the platform. But it does not happen when he collids with the bottom and the other sides.
I am pasting a link with my code https://www.4shared.com/rar/jqa5oRGWei/AULA_6.html
In the stage there are two blue platforms which are ocorrences of "plataforma" symbol. Class Plataforma is an actionscript class automatically generated by the flash for "plataforma" symbol.
Here I am pasting the code
private function enterFrameEvents(event:Event):void{
if(this.hitTestObject(mario)){
// collision with top
if(mario.y + mario.height >= this.y && mario.y <= this.y &&
mario.x + mario.width >= this.x + 5 && mario.x <= this.x + this.width - 5 ){
_root.isJumping = false;
_root.speedY = 0;
mario.y = this.y - mario.height;
}
...
}
I am discount 5 pixels from the corners.
Thank you!
The problem is the mario.height value.
Explanation: This value constantly changes.
Solution: Add a transparent background to mario MovieClip, slightly bigger than the 4 figures, or add 5 pixels to mario.y after the top collision.
For example:
mario.y = this.y - mario.height + 5;

Rotating a canvas object around the mouse cursor

I am trying to replicate this effect: http://hakim.se/experiments/html5/trail/03/
I have this as a Particle constructor:
function Particle(x, y) {
this.x = x;
this.y = y;
this.radius = 4;
this.color = '#f0f';
this.speed = 15;
this.angle = 0;
}
And I'm using this loop to animate all particle instances:
for (var i = 0, len = particles.length; i < len; i++) {
var dx = mouse.x - particles[i].x,
dy = mouse.y - particles[i].y,
angle = Math.atan2(dy, dx);
particles[i].vx = Math.cos(angle) * particles[i].speed;
particles[i].vy = Math.sin(angle) * particles[i].speed;
particles[i].x += particles[i].vx;
particles[i].y += particles[i].vy;
particles[i].draw(ctx);
}
The particles follow the mouse, but reach the cursor and start flickering around it, which is not a desired behaviour. I'd like to circle around the cursor, as in the example.. The interesting part is that if I set the particle.speed to something like 30 and add 1000 to the angle, the particles rotate around the cursor, but really fast and ruin the effect...
You can see a live example here: http://codepen.io/gbnikolov/pen/EwafI
All suggestions are more then welcome, thanks in advance!
P.S. I know that the code for the pointed example is easily findable, but I'm relatively new to javascript and I'm not that good at reading other people code and can't quite understand the logic behind it..
Currently the target of your particles is the mouse cursor. But that's not the target you want. You want a target that's moving around your cursor. And you want for every particle a different target, so they don't hover all at the same place.
There are also some other things that the original does and you don't:
in the original, the particle speed depends on the distance to the target
it seems they can't change the direction instantly, but change the direction of their movement relatively slowly.

how to move polygon points depending on movement of one point?

i have polygon say (Hexagonal with 6 lines) this Hexagonal connected from center with 6 point That make 6 triangles
I need when move any point(cause to move triangles) ,, other points move like this point i mean if the left point move to lift other points move to the left and so on
the code I want like this ptcP1.x and ptcP1.y the point that i moving it other point move depend on ptcP1 movement note that, this equations work fine in square shape ,, put in Penta and hexa ..etc this equations in valid so can any one help me
function button1_triggeredHandler( event:Event ):void
{
mode="mode2";
//trace(list.selectedIndex);
if(list.selectedIndex==1)
{
DrawSqure.ptcP1.x = Math.random() + 50;
DrawSqure.ptcP1.y = Math.random() + 50;
DrawSqure.ptcP2.y = 50-DrawSqure.ptcP1.x;
DrawSqure.ptcP2.x = DrawSqure.ptcP1.y;
DrawSqure.ptcP3.x = 50-DrawSqure.ptcP1.y;
DrawSqure.ptcP3.y = DrawSqure.ptcP1.x;
DrawSqure.ptcP4.x = 50-DrawSqure.ptcP1.x;
DrawSqure.ptcP4.y = 50-DrawSqure.ptcP1.y;
}
As stated in the comments, storing the vertices/points into a container (Array or Vector) and then adjusting those positions when you move is the best way to do it. Here is an example of how that might work:
//setup array or vector of vertices
var polygonVertices:Array = [DrawPolygon.ptcP1, DrawPolygon.ptcP2, DrawPolygon.ptcP3, DrawPolygon.ptcP4];
This method will take all the vertices and apply the translation:
//function for adjusting all the vertices based on the distance you pass
function moveShape( vertices:Array, dx:Number, dy:Number ) {
var i:int;
for ( ; i < vertices.length; i++ ) {
vertices[i].x += dx;
vertices[i].y += dy;
}
}
Then you would just need to know your distance X & Y your shape has moved and you can call moveShape( polygonVertices, 100, 100 );
I inserted 100,100 as the distance parameters as an example, but this should give you the results you are looking for.

Nape Moving Platform

Okay Im relatively new to nape and Im in the process of making a game, I've made a Body called platform of type KINEMATIC, and I simply want to move it back a forth in a certain range on the stage. Can somebody please see where im going wrong , thanks.
private function enterFrameHandler(ev:Event):void
{
if (movingPlatform.position.x <= 150 )
{
movingPlatform.position.x += 10;
}
if (movingPlatform.position.x >= 260)
{
movingPlatform.velocity.x -= 10;
}
}
First of in one of the if blocks you are incrementing position.x by 10 in the other one you are decrementing velocity.x by 10. I guess you meant position.x in both.
Secondly, imagine movingPlatform.position.x is 150 and your enterFrameHandler runs once. movingPlatform.position.x will become 160 and on the next time enterFrameHandler is called none of the if blocks will execute since 160 is neither less than or equal to 150 or greater than or equal to 260.
You can use the velocity to indicate the side its moving and invert it once you go beyond an edge, something like :
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150 || movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -movingPlatform.velocity.x;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
Obviously this might cause problems if the object is already at let's say x=100, it will just keep inverting it's velocity, so either make sure you place it between 150-260 or add additional checks to prevent it from inverting it's direction more than once.
This might be a better way of doing it :
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150) {
movingPlatform.velocity.x = 1;
} else if (movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -1;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
In general:
Kinematic bodies are supposed to be moved solely with velocity, if you change their position directly then they are not really moving as much as they are 'teleporting' and as far as the physics is concerned their velocity is still exactly 0 so things like collisions and friction will not work as you might expect.
If you want to still work with positions instead of velocities, then there's the method setVelocityFromTarget on the Body class which is designed for kinematics:
body.setVelocityFromTarget(targetPosition, targetRotation, deltaTime);
where deltaTime is the time step you're about to use in the following call to space.step();
All this is really doing is setting an appropriate velocity and angularVel based on the current position/rotation, the target position/rotation and the amount of time it should take to get there.

How do I move and scale properly in ActionScript?

function scale(e:Event):void{
plane1_mc.scaleX += .1;
plane1_mc.scaleY += .1;
plane1_mc.x -= 5;
plane1_mc.y -= 3;
}
function prop(evt:Event):void{
plane1_mc.prop_mc.rotation += 100;
}
plane1_mc.prop_mc.addEventListener(Event.ENTER_FRAME, prop);
plane1_mc.addEventListener(Event.ENTER_FRAME, scale);
this is what im using to try to get plane1_mc to scale and move. it was doing both before but now its only doing the scale. Anybody feel free to tell me
Your code is correct. Try increasing the amount you move it and post what happens. Something like this:
plane1_mc.x -= 50;
plane1_mc.y -= 30;
Also make sure you aren't scaling plane1_mc anywhere else. It's possible that another event is being fired instead of this one, so it looks like the scaleX and scaleY values are being modified from this function when they really aren't. I'd suggest putting some trace statements in this function and seeing if they show up.