Converting AS2 to AS3 - actionscript-3

I am new to Flash AS3 and I have seen this code online but its coded in AS2 can anyone help me.
onClipEvent (enterFrame)
{
distance = _root.enemy._x - _root.player._x;
if (distance < 100 && distance > -100 && _root.enemyTimer == 0)
{
a = int(Math.random() * 100);
if (a >= 0 && a < 60)
{
_root.enemy.gotoAndStop('attack1');
_root.healthbar.gotoAndStop(_root.healthbar._currentframe -= 5);
}
else if (a >= 60 && a <= 100)
{
_root.enemy.gotoAndStop('attack2');
_root.healthbar.gotoAndStop(_root.healthbar._currentframe -= 8);
}
}
if (distance < 160 && distance > 100)
{
this._xscale = _root.eScale;
_x -= 2;
}
if (distance > -160 && distance < -100)
{
this._xscale = -_root.eScale;
_x += 2;
}
}
tnx in advance I'll be using this as a reference.

this.addEventListener(Event.ENTER_FRAME, functionName);
function functionName(e:Event):void
{
var distance:Number = this.enemy.x - this.player.x;
if (distance < 100 && distance > -100 && this.enemyTimer == 0)
{
var a:int = int(Math.random() * 100);
if (a >= 0 && a < 60)
{
this.enemy.gotoAndStop('attack1');
this.healthbar.gotoAndStop(this.healthbar.currentFrame -= 5);
}
else if (a >= 60 && a <= 100)
{
this.enemy.gotoAndStop('attack2');
this.healthbar.gotoAndStop(this.healthbar.currentFrame -= 8);
}
}
if (distance < 160 && distance > 100)
{
this.scaleX = this.eScale;
this.x -= 2;
}
if (distance > -160 && distance < -100)
{
this.scaleX = -this.eScale;
this.x += 2;
}
}

Related

Function not functioning properly

I have a question about my function.
This is my function :
function checkKind()
{
var i:Number = new Number;
for (i=0 ; i<9 ; i++)
{
if (pValue[i] >= 1 && pValue[i] <= 13)
{
pKind[i] = 1;
}
else if (pValue[i] >= 14 && pValue[i] <= 26)
{
pKind[i] = 2;
}
else if (pValue[i] >= 27 && pValue[i] <= 39)
{
pKind[i] = 3;
}
else if (pValue[i] >= 40 && pValue[i] <= 52)
{
pKind[i] = 4;
}
}
}
pKind[i] is always filled up with number 1, so pKind[i] = 1.
I've already checked the pValue[i] which is properly filled.
Can someone help me to see what is wrong in my code ?

ActionScript 3.0 TypeError #1009 Null object? but there is no null object?

I am making a game in flash and my only problem so far is this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at farmgame227_fla::MainTimeline/onenter()[farmgame227_fla.MainTimeline::frame2:144]
The line will vary depending on how I move to the next frame. I am 99% sure a null object is not the problem, so I am stumped. I will show my entire code for frame 2 and I would appreciate it so much if anyone could lend some insight.
stop();
import flash.events.Event;
addEventListener(Event.ENTER_FRAME,onenter);
stop();
//starting positions for sheep
sheep1.y = 330
sheep1.x = 350
sheep2.y = 360
sheep2.x = 380
sheep3.y = 300
sheep3.x = 450
sheep4.y = 330
sheep4.x = 350
gold.y = 150
gold.x = 800
goat.y = 620
goat.x = 800
var myscore = 0;
function onenter(e:Event)
{
//hand follows mouse cursor
hand.x = mouseX;
hand.y = mouseY;
//sheep velocity
sheep1.x = sheep1.x + 1;
sheep1.y = sheep1.y - 1;
sheep2.x = sheep2.x - 1;
sheep2.y = sheep2.y - 1;
sheep3.x = sheep3.x + 1;
sheep3.y = sheep3.y + 1;
sheep4.x = sheep4.x - 3;
sheep4.y = sheep4.y + 3;
//sheep1's boundaries
if (sheep1.y < 0 || sheep1.y > 775 || sheep1.x < 0 || sheep1.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//sheep2's boundaries
if (sheep2.y < 0 || sheep2.y > 775 || sheep2.x < 0 || sheep2.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//sheep3's boundaries
if (sheep3.y < 0 || sheep3.y > 775 || sheep3.x < 0 || sheep3.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//sheep4's boundaries
if(sheep4==null)
{
trace("4 it's null");
}
if (sheep4.y < 0 || sheep4.y > 775 || sheep4.x < 0 || sheep4.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//hand hitting sheep, sending them back
if(sheep1==null)
{
trace("1 is null");
}
if(hand==null)
{
trace("hand is null");
}
if(fence==null)
{
trace("fence is null");
}
if (hand.hitTestObject (sheep1))
{
sheep1.y = 330;
sheep1.x = 350;
myscore = myscore + 1;
}
if (hand.hitTestObject (sheep2))
{
sheep2.y = 360;
sheep2.x = 380;
myscore = myscore + 1;
}
if (hand.hitTestObject (sheep3))
{
sheep3.y = 300;
sheep3.x = 450;
myscore = myscore + 1;
}
if (hand.hitTestObject (sheep4))
{
sheep4.y = 330;
sheep4.x = 350;
myscore = myscore + 1;
}
if (hand.hitTestObject (gold))
{
gold.x = 2500;
gold.y = 610;
gold.x = gold.x - 9;
myscore = myscore + 5;
}
if (hand.hitTestObject (goat))
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//hand touching fence, results in game over
if (hand.hitTestObject (fence))
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
score.text = myscore;
//changing sheep speed based on score
//score 40 and 55 should be next
if (myscore >= 12)
{
sheep2.x = sheep2.x - 2;
sheep2.y = sheep2.y - 2;
}
if (myscore >= 24)
{
sheep3.x = sheep3.x + 2;
sheep3.y = sheep3.y + 2;
}
if (myscore >= 30)
{
sheep1.x = sheep1.x + 3;
sheep1.y = sheep1.y - 3;
}
//initiating and controlling gold and goat
if (myscore > 17)
{
gold.x = gold.x - 8;
}
if (gold.x < -9)
{
gold.x = 2000;
gold.y = 610;
gold.x = gold.x - 9;
}
if (myscore > 8)
{
goat.x = goat.x - 3;
}
if (goat.x < -9)
{
goat.x = 1600;
goat.y = 150;
goat.x = goat.x - 4;
}
}
You should shift all the checks in onenter upwards, prior to assigning anything to either sheep or other objects. Then you will likely find that one of the objects is null. The #1009 error means that some object reference that's placed right before a "." qualifier is null, and you can't get properties out of nothing.
Also, try placing return right after any call of nextFrame(), because when you perform nextframe, the previous frame's vars are invalidated, thus you can't refer to say hand because there's no more hand, it's destroyed along with the whole previous frame.

AS3 problems with cos / sin - doesn't fire in the direction i want

Ok so i have a space shooter game, seen from above. The ship travels upward (-y) and rotates slightly when moved left and right with the mouse. The maximum rotation is between -10 and 10, with easing of * 0.5.
To get the "bullets" to fire in the same direction the ship is facing, i am trying to use the cos sin formulas. It works great as long as the ship's rotation is -5 or 5 (-10 * 0.5, 10 * 0.5), but as soon as the rotation approaches 0, it starts firing all over the place:)
I have read that i need to convert degrees into radians to be able to use cos and sin, and i have tried that - the best result i got from that was that it only fired left or right, depending on the rotation, with no variation in angle.
Now im new to AS of course, so apologize for comprehensible code:)
Can anyone help me figure out why the angle works fine at -5 and 5, but gets crazy when it closes in on 0?
Thanks guys!
import flash.events.KeyboardEvent;
import flash.events.Event;
var spaceship_mc:spaceship = new spaceship();
stage.addChild(spaceship_mc);
spaceship_mc.x = stage.stageWidth / 2;
spaceship_mc.y = stage.stageHeight / 2;
stage.addEventListener(Event.ENTER_FRAME,shipMovement);
function shipMovement(event:Event):void
{
var dx:int = spaceship_mc.x - mouseX;
var dy:int = spaceship_mc.y - mouseY;
spaceship_mc.x -= dx / 15;
spaceship_mc.y -= dy /20;
if (dx >= -10 && dx <= 0)
{
spaceship_mc.rotation = -dx * 0.5;
}
else if (dx >= 0 && dx <= 10)
{
spaceship_mc.rotation = dx * -0.5;
}
else if (dx > -10)
{
spaceship_mc.rotation = -10 * 0.5;
}
else if (dx < 10)
{
spaceship_mc.rotation = 10 * 0.5;
}
if (spaceship_mc.rotation > 0)
{
spaceship_mc.scaleX = 1-(0.05 * spaceship_mc.rotation);
}
if (spaceship_mc.rotation < 0)
{
spaceship_mc.scaleX = 1-(0.05 * -(spaceship_mc.rotation));
}
if (dy >= 0 && dy < 22 )
{
spaceship_mc.afterburner_mc.y = 22;
}
else if (dy >= 22 && dy <= 32)
{
spaceship_mc.afterburner_mc.y = dy;
}
else if (dy > 32)
{
spaceship_mc.afterburner_mc.y = 32;
}
else if (dy < 0)
{
spaceship_mc.afterburner_mc.y = 22;
}
var spaceshipHalfWidth:uint = spaceship_mc.width/2;
var spaceshipHalfHeight:uint = spaceship_mc.height/2;
if (spaceship_mc.x + spaceshipHalfWidth > stage.stageWidth)
{
spaceship_mc.x = stage.stageWidth - spaceshipHalfWidth;
}
else if (spaceship_mc.x - spaceshipHalfWidth < 0)
{
spaceship_mc.x = 0 + spaceshipHalfWidth;
}
if (spaceship_mc.y - spaceshipHalfHeight < 0)
{
spaceship_mc.y = 0 + spaceshipHalfHeight;
}
else if (spaceship_mc.y + spaceshipHalfHeight > stage.stageHeight)
{
spaceship_mc.y = stage.stageHeight - spaceshipHalfHeight;
}
}
addEventListener(MouseEvent.CLICK, skyt);
function skyt(e:MouseEvent):void
{
nyttSkudd();
}
function nyttSkudd()
{
var leftPhoton_mc:photon = new photon();
var rightPhoton_mc:photon = new photon();
leftPhoton_mc.x = spaceship_mc.x - 25;
leftPhoton_mc.y = spaceship_mc.y - 10;
rightPhoton_mc.x = spaceship_mc.x + 16;
rightPhoton_mc.y = spaceship_mc.y - 10;
leftPhoton_mc.rotation = spaceship_mc.rotation;
rightPhoton_mc.rotation = spaceship_mc.rotation;
addChild(leftPhoton_mc);
addChild(rightPhoton_mc);
addChild(spaceship_mc);
leftPhoton_mc.addEventListener(Event.ENTER_FRAME, skudd);
rightPhoton_mc.addEventListener(Event.ENTER_FRAME, skudd);
}
function skudd(e:Event):void
{
var photonAngle = e.target.rotation;
var photonSpeed = 10;
if (photonAngle == 0)
{
e.target.y -= photonSpeed;
}
else if (photonAngle > 0)
{
e.target.x += photonSpeed * Math.cos(photonAngle);
e.target.y += photonSpeed * Math.sin(photonAngle);
}
else if (photonAngle < 0)
{
e.target.x -= photonSpeed * Math.cos(photonAngle);
e.target.y -= photonSpeed * Math.sin(photonAngle);
}
if (e.target.y < 0 || e.target.y > 400)
{
e.target.removeEventListener(Event.ENTER_FRAME, skudd);
e.target.parent.removeChild(e.target);
}
}
photonAngle is in degrees but Math.sin/cos takes radians. so you need the following conversion.
var photonAngle = e.target.rotation * Math.PI / 180.0;
also
e.target.x += photonSpeed * Math.cos(photonAngle);
e.target.y += photonSpeed * Math.sin(photonAngle);
is sufficient you don't need the if/else statements:
if (photonAngle == 0)
{
e.target.y -= photonSpeed;
}
else if (photonAngle > 0)
{
e.target.x += photonSpeed * Math.cos(photonAngle);
e.target.y += photonSpeed * Math.sin(photonAngle);
}
else if (photonAngle < 0)
{
e.target.x -= photonSpeed * Math.cos(photonAngle);
e.target.y -= photonSpeed * Math.sin(photonAngle);
}
good luck!

HTML5: Get click position from canvas arc

See this jsFiddle post for a working arc drawing; thanks to Simon Sarris for the fix in my previous questions.
I'm using the KineticJS plugin to create shapes and make use of event handlers. Assuming you clicked somewhere on the arc and the arc knew where you clicked (x, y), how could those 2 coordinates be used to determine a percentage?
When you click anywhere, the total percentage is always 100%.
Addin
To make this simpler, what could I do to (x, y) to virtually bend the object so that x goes from 0 to maximum x?
Simple trigonometry. sin(angle) = opposite / adjacent. opposite is the y value and adjacent is the x value. So Math.asin((xx - x) / (yy - y)) where xx and yy are the coords of the centre of the arc. That gives you the angle, which you can then divide 2 * Math.PI by.
Off the top of my head I can't remember what happens with negative numbers. You may have to take the Math.abs value of the arguments, then work out which quadrant the click is in (easy to do by using < and >) and add Math.PI / 2 for each one.
This includes a check if the mouse is inside the arc:
// Return range is 0 to Math.PI * 2
function get_mouse_circle_angle(origin_x, origin_y, mouse_x, mouse_y) {
var mouse_angle = Math.atan2(mouse_y - origin_y, mouse_x - origin_x);
if (mouse_angle < 0) {
mouse_angle = (Math.PI * 2) + mouse_angle;
}
return mouse_angle;
}
// Return range is [0, 1)
// 0/1 is 3 oclock
function get_mouse_circle_percent(origin_x, origin_y, mouse_x, mouse_y) {
var mouse_angle = get_mouse_circle_angle(origin_x, origin_y, mouse_x, mouse_y);
return mouse_angle / (2 * Math.PI);
}
function get_mouse_arc_pos(origin_x, origin_y, mouse_x, mouse_y, radius, thickness) {
var mouse_angle = Math.atan2(mouse_y - origin_y, mouse_x - origin_x);
if (mouse_angle < 0) {
mouse_angle = (Math.PI * 2) + mouse_angle;
}
var mouse_percent = mouse_angle / (2 * Math.PI);
var circle_edge_x = origin_x + (radius + thickness / 2) * Math.cos(mouse_angle);
var circle_edge_y = origin_y + (radius + thickness / 2) * Math.sin(mouse_angle);
var arc_inside_x = origin_x + (radius - thickness / 2) * Math.cos(mouse_angle);
var arc_inside_y = origin_y + (radius - thickness / 2) * Math.sin(mouse_angle);
var is_in_circle = true;
if (mouse_angle <= (2 * Math.PI) * 0.25) {
if (mouse_x > circle_edge_x || mouse_y > circle_edge_y)
is_in_circle = false;
}
else if (mouse_angle <= (2 * Math.PI) * 0.5) {
if (mouse_x < circle_edge_x || mouse_y > circle_edge_y)
is_in_circle = false;
}
else if (mouse_angle <= (2 * Math.PI) * 0.75) {
if (mouse_x < circle_edge_x || mouse_y < circle_edge_y)
is_in_circle = false;
}
else {
if (mouse_x > circle_edge_x || mouse_y < circle_edge_y)
is_in_circle = false;
}
var is_in_arc = is_in_circle;
if (is_in_circle) {
if (mouse_angle <= (2 * Math.PI) * 0.25) {
if (mouse_x < arc_inside_x || mouse_y < arc_inside_y)
is_in_arc = false;
}
else if (mouse_angle <= (2 * Math.PI) * 0.5) {
if (mouse_x > arc_inside_x || mouse_y < arc_inside_y)
is_in_arc = false;
}
else if (mouse_angle <= (2 * Math.PI) * 0.75) {
if (mouse_x > arc_inside_x || mouse_y > arc_inside_y)
is_in_arc = false;
}
else {
if (mouse_x < arc_inside_x || mouse_y > arc_inside_y)
is_in_arc = false;
}
}
return {
angle: mouse_angle,
percent: mouse_percent,
is_in_circle: is_in_circle,
is_in_arc: is_in_arc
};
}
Did not really test it, but technically, it should work:
// Where x1 and y1 should be the coordinates of the arc's center
function angle(x1, y1, x2, y2) {
// Calculate a ยท b
var nominator = x1 * x2 + y1 * y2;
// Calculate ||a|| ||b||
var denominator = Math.sqrt(x1*x1 + y1*y1) * Math.sqrt(x2*x2 + y2*y2);
if (denominator == 0) return 0; // Indifinite angle
// Return the angle
return Math.acos(nominator / denominator);
}
// Returns a percent, might be negative
var percent = angle(0, 0, mouseX, mouseY) / (2*Math.PI);
Edit
For negative numbers you can try adding 1 since it's in [-1, 1] range
if (percent < 0) percent += 1;

Gradient masking flash

i want to put gradient masking effect in my 20 images.
var arr:Array = ["m1.jpg","m2.jpg","m3.jpg","m4.jpg","m5.jpg"];
for(var i=0; i<20; i++){
duplicateMovieClip(ho, "newImg_mc"+i , this.getNextHighestDepth());
if(i < 5){
this["newImg_mc"+i]._x = 0;
this["newImg_mc"+i]._y = 110*i;
this["newImg_mc"+i].loadMovie(arr[i]);
}
if(i >= 5 and i < 10){
this["newImg_mc"+i]._x = 110;
this["newImg_mc"+i]._y = 110*(i-5);
this["newImg_mc"+i].loadMovie(arr[i-5]);
}
if(i >= 10 and i < 15){
this["newImg_mc"+i]._x = 220;
this["newImg_mc"+i]._y = 110*(i-10);
this["newImg_mc"+i].loadMovie(arr[i-10]);
}
if(i >= 15 and i < 20){
this["newImg_mc"+i]._x = 330;
this["newImg_mc"+i]._y = 110*(i-15);
this["newImg_mc"+i].loadMovie(arr[i-15]);
}
}
you need to set the blendMode of the mask and the masked object to BlendMode.LAYER.