AS3 - Shooting arrows / targeting - actionscript-3

I am creating a game where fairies cross the screen (bottom to top). There is a laser at the bottom of the screen that goes from right to left. I want to shoot the laser, and have the laser bullets to go from bottom to top. The code that I have has it going from top to bottom. I have tried to reverse the direction by changing out the + = 10 to - = 10... This did get the bullets in the right direction, however, it started it towards the top of my screen.
stage.addEventListener(MouseEvent.MOUSE_DOWN, targeting)
function targeting(e:MouseEvent):void{
var newArrow:blackArrow = new blackArrow();
addChild(newArrow);
newArrow.y = 50;
newArrow.x = shooterMC.x;
newArrow.addEventListener(Event.ENTER_FRAME, shoot);
}
function shoot(e:Event):void {
var arrowMC:MovieClip = MovieClip(e.target);
arrowMC.y += 10;
}
Thank you in advance for any help that you can give me.

You have to change this line to your desired position you want to start the laser animation from:
newArrow.y = 50;
The coordinate system starts in the upper left corner. So the lower right corner is window width | window height.

Related

scroll to the right or left if needed

I'm making a platform game.
My game level is very large and I'd like to make the gameLevel scroll when the player is walking.
So far it's scrolling, but the scrolling speed is not fast enough and when the player is walking to the right, he goes out of the screen.
Do you know how I can speed up the scrolling in order to prevent the character to exceed the middle of the screen ?
Here's my code for the scrolling screen :
// scroll to the right or left if needed
public function scrollWithHero() {
var stagePosition:Number = gamelevel.x+hero.mc.x;
var rightEdge:Number = 400-edgeDistance;
var leftEdge:Number = edgeDistance;
if (stagePosition > rightEdge) {
gamelevel.x -= (stagePosition-rightEdge);
if (gamelevel.x < -(gamelevel.width-stage.stageWidth)) gamelevel.x = -(gamelevel.width-stage.stageWidth);
}
if (stagePosition < leftEdge) {
gamelevel.x += (leftEdge-stagePosition);
if (gamelevel.x > 0) gamelevel.x = 0;
}
}
There are two ways you can scroll a screen.
The first would be to scroll at the exact same speed as the player's movement. which would mean that you do not really move the player, but rather scroll the world (the player would remain in the exact same place on-screen). This is not a very "elegant" way to go, but many games use it and it works.
The second (and better, IMHO) way would be to define some area around the center of the screen and allow the player to move around within this area. Only once the player reaches the end (to left/right) of this area do you start scrolling. This provides a much smoother effect.
So to implement the area, you need a size for it (lets say 100 as an example) and 300 is the center of the screen. So in this case your "area" would be from 250 to 350. Now you will need to detect when the player moves beyond this area and then scroll (instead of moving the player). This can be implemented as follows:
const CENTER:Number = 300; // center position on screen
const SIZE:Number = 100; // size of the movement area
const LEFT:Number = CENTER - (SIZE/2); // left of movement area
const RIGHT:Number = CENTER + (SIZE/2); // right of movement area
public function scrollWithHero() {
var scrollAmount:Number = 0; // holds amount to scroll by (init to none)
if ( hero.mc.x < LEFT ) { // if left of movement area reached
scrollAmount = LEFT - hero.mc.x; // calculate scroll amount
hero.mc.x = LEFT; // keep player at left of movement area
}
else if ( hero.mc.x > right ) { // else if right of movement area reached
scrollAmount = hero.mc.x - right; // calculate scroll amount
hero.mc.x = RIGHT; // keep player at right of movement area
}
// here you can use scrollAmount to scroll the level, and the player's
// position to calculate their offset into your level
}
This is a basic example just to calculate the relevant values, you will still need to use them, but this should illustrate the idea of how the scrolling works.
You should experiment with the size to find an area that feels good for your implementation. Also, if you set SIZE=0 then the player will remain in the center of the screen (i.e. it will work as described in the first implementation).

Make disappear image on the edge of rectangle

I have to move some pictures in my flash. So I have got a background image in my main MovieClip(which I get by Loader class). Inside the image I have rectangles. I'm going to put small image in this rectangle and move it. I need the small image slowly disappear while crossing the rectangle boundaries.
I tried to put another movieclip in rectangles and moved image in this movieclip. But while crossing the rectangle the image didnt disappear. The image just continued its motion without disappearing.
How can I make dissapearing of image while crossing rectangle boundaries?
Sorry for my English.
Get TweenLite. It's an animation "tweening" library that makes animation a breeze. There are others, but this is the one I use.
It depends on the methodology you employ to move and detect your overlaps of image & rectangles.
Let's imagine you have two squares (red square, and blue square) and you want red square to fade-out whenever it overlaps blue square. Is this controlled with the mouse, keyboard, or a pre-calculated move that performs a guaranteed eclipse? Is the fade a factor of the percentage of overlap, or a straight-up 0-to-100 timed transition the moment it comes in contact with blue square? It's not clear from the description you gave as to what exactly you expect your code to do. Please review SO's "Asking" section, to help improve the quality of your question so that you get the right answer you're looking for.
That said, here's one way you could resolve the issue:
import com.greensock.*;
// Create some sample red & blue squares
var red:Sprite = new Sprite();
red.graphics.beginFill(0xFF0000, 1);
red.graphics.drawRect(0, 0, 100, 100);
red.graphics.endFill();
addChild(red);
stage.addEventListener(MouseEvent.MOUSE_MOVE, updateRed);
var blue:Sprite = new Sprite();
blue.graphics.beginFill(0x0000FF, 1);
blue.graphics.drawRect(0, 0, 100, 100);
blue.graphics.endFill();
addChild(blue);
blue.x = 200;
blue.y = 100;
var overlap:Boolean = false; // global state tracker
function updateRed(e:MouseEvent):void {
// Position the red square every time the mouse moves
red.x = stage.mouseX - red.width/2; // center relative to red square's dimensions
red.y = stage.mouseY - red.height/2;
if (red.hitTestObject(blue) && overlap != true) {
// Make sure we only animate on the initial overlap
overlap = true;
TweenLite.to(red, 1, {alpha:0});
} else if (red.hitTestObject(blue) == false && overlap) {
// And converserly, on the initial exit
overlap = false;
TweenLite.to(red, 1, {alpha:1});
}
}

How to position an rotated MovieClip?

It's very easy to position a sprite object in relation to another sprite object, if you don't mess with the rotation. As the title states I want to know how to position 2 MC in relation to an already rotated MC, so they are aligned to each other. The big rocket is at an angle of 35 degrees. First i set the angle of the big rocket to 0 degree, I am adding the 2 small rockets on the stage and position them on each side of the bigger rocket. So far so good... I am rotating everything back to the initial angle (35 degrees), but something is not right, as you can see the results in pic1 What must I change, so that the 2 small rockets stays perfectly aligned (one on the left side, the other on the right side of the bigger rocket) and rotated, as in pic2? The registration point for all objects is in the upper-left corner. Edit: The 2 small rockets must reside outside the bigger rocket container, because, eventually they will be animated independently from the container.
pic1
pic2
var _rot = rocketShip.rotation;
rocketShip.rotation = 0;
addChild(_leftRocket);
addChild(_rightRocket);
_leftRocket.x = rocketShip.x - _leftRocket.width;
_leftRocket.y = rocketShip.y + 20;
_rightRocket.y = rocketShip.x + _rightRocket.width;
_rightRocket.y = rocketShip.y + 20;
rocketShip.rotation = _rot;
_leftRocket.rotation = _rightRocket.rotation = rocketShip.rotation;
Try to move _leftRocket and _rightRocket to the right position after rotation.
Here is the code move _leftRocket
//the old left bottom point of rocketShip
var point1:Point= new Point(0, rocketShip.height);
//the old right-bottom poiny of leftship
var point2:Point = new point(_leftRocket.width, _leftRocket.height);
//the old right bottom point of rocketShip
var point5:Point= new Point(rocketShip.width, rocketShip.height);
//the old left-bottom point of rightship's
var point6:Point = new point(0, _leftRocket.height);
//.. Your code
//get the new position of rocketship's left bottom point after rotation,leftShip's right bottom corner will be in this point
var point3:Point = rocketShip.transform.matrix.transformPoint(point1);
//get the new point of leftship's right-bottom-corner after rotation.
var point4:Point = _leftRocket.transform.matrix.transformPoint(point2);
//move the leftShip
_leftRocket.x -= point4.x - point3.x;
_leftRocket.y -= point4.y - point3.y;
Here to for right ship
//get the new position of right bottom point of rocketShip after rotation,rightShip's left bottom corner will be in this point
var point7:Point = rocketShip.transform.matrix.transformPoint(point5);
//get the new point of rightship's left-bottom-corner after rotation.
var point8:Point = _rightRocket.transform.matrix.transformPoint(point6);
//move the rightShip
_rightRocket.x -= point8.x - point7.x;
_rightRocket.y -= point8.y - point7.y;
And I think you should Change
_rightRocket.y = rocketShip.x + _rightRocket.width;
To
_rightRocket.x = rocketShip.x + rocketShip.width;

startdrag start point limitations?

I have a slider that controls a movie clip by dragging it through the frames to animate and this works great when dragging from left to right, But i want the slider to start in the middle and drag through the movie clip from a center point being able to go 350 to the right and 350 to the left.
Is this possible?
Heres my code so far and as you can see it drags 350 to the right through dialSpin_mc.
Is there a way to make the slider start at a certain frame and go backwards and forwards?
dialSpin_mc.stop();
slider_mc.knob_mc.buttonMode = true;
slider_mc.knob_mc.addEventListener(MouseEvent.MOUSE_DOWN, onDragKnob);
stage.addEventListener(MouseEvent.MOUSE_UP, onReleaseKnob)
slider_mc.knob_mc.buttonMode = true;
function onDragKnob(myEvent:Event):void
{
slider_mc.knob_mc.startDrag(false, new Rectangle(0,0,350,0));
slider_mc.knob_mc.addEventListener(Event.ENTER_FRAME, onScrubMovie);
}
function onReleaseKnob(myEvent:Event):void
{
slider_mc.knob_mc.stopDrag();
slider_mc.knob_mc.removeEventListener(Event.ENTER_FRAME, onScrubMovie);
}
function onScrubMovie(myEvent:Event):void {
var playHead:int=Math.round((slider_mc.knob_mc.x/350*8)+1);
dialSpin_mc.gotoAndStop(playHead);
}
As discussed in comments this is how you could code the slider functionality without using the startDrag() and stopDrag() functions.
The idea is that when you press the mouse button down over the slider, an ENTER_FRAME listener is created. Every frame the sliders X position will be updated to match the mouseX position, and to make sure it can only travel 175 pixels either way we also check the sliders new position.
After making sure the sliders position is valid, you can use the same code to set the dialSpin_mc frame.
Once the mouse button is released, the enter frame listener is removed.
The sliderOrigin declared at the top of the code will need to be changed to whatever is appropriate for your project (whatever the sliders xposition is when you move it to the middle of the slide area rather than the very left).
var sliderOrigin:int = 150; // The x position the slider is in when in the center of the slide bar
slider_mc.x = sliderOrigin;
slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
slider_mc.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mDown(e:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, UpdateDrag);
}
function mUp(e:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, UpdateDrag);
}
function UpdateDrag(e:Event):void
{
slider_mc.x = mouseX;
// Check if the slider is 'out of bounds' and change its position if it is
if(slider_mc.x < sliderOrigin - 175)
slider_mc.x = sliderOrigin - 175;
else if(slider_mc.x > sliderOrigin + 175)
slider_mc.x = sliderOrigin + 175
var playHead:int = Math.round((slider_mc.x/350*8)+1);
dialSpin_mc.gotoAndStop(playHead);
}
Depending on the start position of the slider the range of playHead will change (so if the slider starts at x pos 200 the range would be between 2 and 10, at x pos 400 the range is between 6 and 14 - simple fix by changing the offset from +1 to whatever is necassary).

as3 finding the offset of a block

I am trying to make a pong like game but i need to find the offset from the center of the paddle so that i can make it bounce differently depending on where it hits the paddle. How can i achieve this?
Try:
// get the center of the paddle
var centerXpaddle = paddle.x - paddle.width/2;
var centerYpaddle = paddle.y -paddle.height/2;
if (objectThatHits.hitTestObject(paddle)) {
// get the current point of object
var offsetX = objectThatHits.x - centerXpaddle;
var offsetY = objectThatHits.y - centerYpaddle;
}
The first two lines get the center of the the paddle (it's current position minus half the width and half the height) and the second part is a "test" for when the "object that hits" hits the "paddle".