Making an HTML Jigsaw Puzzle in Flash CC [duplicate] - html

I am am doing this for a interactive assignment for a media arts class and I have no idea how to code in action script 3. I took the orignal code from a tutorial and it didn't work so I came here and attempted to learn how to modify it.
AS3 Code
//*********************
// Initialize:
flash.events.MouseEvent
var numPieces = 16;
for (var i = 0; i < numPieces; i++)
{
var pieceName = "p" + (i + 1);
var piece = this[pieceName];
if( piece ){
piece.name = pieceName;
piece.addEventListener(MouseEvent.MOUSE_DOWN, function)(evt)
{
this.scaleX = 1;
this.scaleY = 1;
this.shadow = null;
this.parent.addChild(this);// Bump to top
this.offset = {x:this.x - evt.stageX, y:this.y - evt.stageY};
});
piece.addEventListener(MouseEvent.MOUSE_MOVE, function)
{
this.x = evt.stageX + this.offset.x;
this.y = evt.stageY + this.offset.y;
});
piece.addEventListener(MouseEvent.MOUSE_UP, function)(evt)
{
var target = this.parent["t"+this.name.substr(1)];
if( target && hitTestInRange( target, 30) ){
this.x = target.x;
this.y = target.y;
}
});
}
}
function hitTestInRange( target, range )
{
if( target.x > stage.mouseX - range &&
target.x < stage.mouseX + range &&
target.y > stage.mouseY - range &&
target.y < stage.mouseY + range )
{
return true;
}
return false;
}
Please help me fix this code so I can run my puzzle and move the pieces.
I can upload the flash file if needed

Related

Actionscript 3 error 1009: Cannot access a property or method of a null object reference

I'm trying to make a simple game on Animate CC. Everything seems to work fine except when I look in the output, I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
at _2D_CW2_Game_v10_8_fla::MainTimeline/move()
at _2D_CW2_Game_v10_8_fla::MainTimeline/updateOb()
So I know where the issue might be, and I've been trying tweaking the code for days, googling possible solutions, but to no avail...
My entire source code is as below. Any feedback/suggestions will be greatly appreciated.
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundMixer;
//==================================================
// Variable declaration
//==================================================
// defines the variables for boundaries
var left:Number = 0;
var top:Number = 0;
var right:Number = stage.stageWidth;
var bottom:Number = stage.stageHeight;
var velX:Number = 0;
var velY:Number = 0;
var gravity:Number = 1;
var friction:Number = 0.8;
var bounce:Number = -0.5;
var score:Number = 2;
var cv:Number = 0;
var curCount:Number = 30; // countdown 30s
var rightKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var touchGround:Boolean = false;
// create and place player object on stage
var player:Player = new Player();
player.x = 110;
player.y = 460;
addChild(player);
// create obstacle array
var obstacles:Array = new Array();
var numOb:Number = 3;
// create and place enemies on stage
for (var i:Number = 0; i < numOb; i++) {
var ob:Npc = new Npc();
ob.x = 800;
ob.y = 470;
ob.scaleX = -1;
ob.vx = Math.random() * 20 + 1;
addChild(ob);
obstacles.push(ob);
}
//==================================================
// Event handlers
//==================================================
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
addEventListener(Event.ENTER_FRAME, updateOb);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
//==================================================
// Functions
//==================================================
function keyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.RIGHT) {
rightKeyDown = true;
}
if (e.keyCode == Keyboard.LEFT) {
leftKeyDown = true;
}
if (e.keyCode == Keyboard.UP) {
// if player isn't already jumping and is on the ground
if (!upKeyDown && touchGround) {
// then start jumping
isJumping();
}
upKeyDown = true;
}
}
function keyUp(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.RIGHT) {
rightKeyDown = false;
}
if (e.keyCode == Keyboard.LEFT) {
leftKeyDown = false;
}
if (e.keyCode == Keyboard.UP) {
upKeyDown = false;
}
}
function EntFrame(e:Event):void {
player.x += velX;
player.y += velY;
velX *= friction;
velY += gravity;
if (player.y >= 450) {
touchGround = true;
player.y = 450;
}
// boundary checks
if (player.x + player.width/2 > right) {
player.x = right - player.width/2;
player.velX *= bounce;
} else if (player.x - player.width/2 < left) {
player.x = left + player.width/2;
player.velX *= bounce;
}
// make player move left or right
controls();
if (curCount > 0) {
cv++;
if (cv >= 30) {
curCount--;
cv = 0;
timertext.text = String(curCount);
if (curCount == 0) {
restart();
gotoAndStop("gameOverWon");
}
}
}
}
function updateOb(e:Event):void {
// make obstacles move
for (var i:Number = 0; i < numOb; i++) {
var ob:Npc = obstacles[i];
move(ob);
if (player.hitTestObject(obstacles[i])) {
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y - player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y - player.height/2, true))*/
bumpOb(obstacles[i]);
}
}
scoretext.text = String(score);
if (score == 0) {
restart();
gotoAndStop("gameOverLost");
}
}
// applies basic velocity to enemies
function move(moveOb) {
moveOb.x -= moveOb.vx;
if (moveOb.x + moveOb.width/2 > right) {
moveOb.x = right - moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = -1;
}
if (moveOb.x - moveOb.width/2 < left) {
moveOb.x = left + moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = 1;
}
}
function bumpOb(p) {
if (p) {
p.removeEventListener(Event.ENTER_FRAME, updateOb);
if (p.parent) {
removeChild(p);
score--;
}
}
}
function restart() {
if(contains(player)) {
removeChild(player);
}
for (var i:int = 0; i < numOb; i++) {
if (contains(obstacles[i]) && obstacles[i] != null) {
removeChild(obstacles[i]);
obstacles[i] = null;
}
}
// returns a new array that consists of a range of elements from the original array,
// without modifying the original array
obstacles.slice(0);
}
function controls() {
if (rightKeyDown) {
velX += 3;
player.scaleX = 1;
}
if (leftKeyDown) {
velX -= 3;
player.scaleX = -1;
}
}
function isJumping() {
touchGround = false;
velY = -15;
}
//==================================================
// Sound control for background music
//==================================================
btnMute.addEventListener(MouseEvent.CLICK, mute);
function mute(e:MouseEvent):void {
SoundMixer.soundTransform = new SoundTransform(0);
btnMute.removeEventListener(MouseEvent.CLICK, mute);
btnMute.addEventListener(MouseEvent.CLICK, unmute);
}
function unmute(e:MouseEvent):void {
SoundMixer.soundTransform = new SoundTransform(1);
btnMute.removeEventListener(MouseEvent.CLICK, unmute);
btnMute.addEventListener(MouseEvent.CLICK, mute);
}
I had the same problem when I created interactive elements for animation. Check which layer the interactive object is on. A similar error occurred when the object overlapped something that was located on the layer above.
You can try...
1) Your Npc is a class/library object, right?
Give the source MC/Sprite, the instance name of moveOb or p.
2) or else try... Use function parameters (this is a better coding style):
(2a) Since you say..
var ob:Npc = obstacles[i];
move(ob);
ps: why not simplify (without var) as : move( obstacles[i] ); ...?
(2b) Your move function should specify a data type together with your parameter name...
//# applies basic velocity to enemies
//# Wrong...
//function move(moveOb) {
//# Better...
function move( moveOb : Npc ) {
//# Aso fix as...
function bumpOb(p : Npc ) {
By using function parameters, you can now give unique names to the (function's) input parameters but stay referencing the same (or compatible) data type.
Let me know how it goes.
The obstacles array may have null elements in the middle. What if you add a condition to continue if it's null?
function updateOb(e:Event):void {
// make obstacles move
for (var i:Number = 0; i < obstacles.length; i++) {
var ob:Npc = obstacles[i];
if(!ob) continue;
move(ob);
if (player.hitTestObject(ob)) {
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y -
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y -
player.height/2, true))*/
bumpOb(obstacles[i]);
}
}
scoretext.text = String(score);
if (score == 0) {
restart();
gotoAndStop("gameOverLost");
}
}

Particle's in AS3

I'm currently using this particle system and it work fine. Particle's should be destroy after cross boundry's or alpha's smaller then 0. But however sometimes when i use this code like pereatly 4 it fails and particles cant destory.
function init():void
{
particleArray = [];
addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);
createParticle(s3.whell.x,s3.whell.y);
}
function onEnterFrameLoop(event:Event):void
{
updateParticle();
}
/**
* createParticle(target X position, target Y position)
*/
function createParticle(targetX:Number, targetY:Number):void
{
//run for loop based on particleTotal
for (var i:Number = 0; i < particleTotal; i++)
{
var particle_mc:MovieClip = new Particle();
//set position & rotation, alpha
particle_mc.x = targetX
particle_mc.y = targetY
particle_mc.rotation = Math.random() * 360;
particle_mc.alpha = Math.random() * 1.1;
//set particle boundry
particle_mc.boundyLeft = targetX - particleRange;
particle_mc.boundyTop = targetY - particleRange;
particle_mc.boundyRight = targetX + particleRange;
particle_mc.boundyBottom = targetY + particleRange;
//set speed/direction of fragment
particle_mc.speedX = Math.random() * particleMaxSpeed - Math.random() * particleMaxSpeed;
particle_mc.speedY = Math.random() * particleMaxSpeed - Math.random() * particleMaxSpeed;
particle_mc.speedX *= particleMaxSpeed
particle_mc.speedY *= particleMaxSpeed
//set fade out speed
particle_mc.fadeSpeed = Math.random()*particleFadeSpeed;
//just a visual particle counter
particleCurrentAmount++;
// add to array
particleArray.push(particle_mc);
// add to display list
addChild(particle_mc);
}
}
function updateParticle():void
{
for (var i = 0; i < particleArray.length; i++)
{
var tempParticle:MovieClip = particleArray[i];
//update alpha, x, y
tempParticle.alpha -= tempParticle.fadeSpeed;
tempParticle.x += tempParticle.speedX;
tempParticle.y += tempParticle.speedY;
// if fragment is invisible remove it
if (tempParticle.alpha <= 0)
{
destroyParticle(tempParticle);
}
// if fragment is out of bounds, increase fade out speed
else if (tempParticle.x < tempParticle.boundyLeft ||
tempParticle.x > tempParticle.boundyRight ||
tempParticle.y < tempParticle.boundyTop ||
tempParticle.y > tempParticle.boundyBottom)
{
tempParticle.fadeSpeed += 8;
destroyParticle(tempParticle);
}
}
}
function destroyParticle(particle:MovieClip):void
{
for (var i = 0; i < particleArray.length; i++)
{
var tempParticle:MovieClip = particleArray[i];
if (tempParticle == particle)
{
particleCurrentAmount--;
particleArray.splice(i,1);
removeChild(tempParticle);
}
}
}

Can't interact with tiles in an interactive jigsaw puzzle (Flash CC AS3)

I am am doing this for a interactive assignment for a media arts class and I have no idea how to code in action script 3. I took the orignal code from a tutorial and it didn't work so I came here and attempted to learn how to modify it.
AS3 Code
//*********************
// Initialize:
flash.events.MouseEvent
var numPieces = 16;
for (var i = 0; i < numPieces; i++)
{
var pieceName = "p" + (i + 1);
var piece = this[pieceName];
if( piece ){
piece.name = pieceName;
piece.addEventListener(MouseEvent.MOUSE_DOWN, function)(evt)
{
this.scaleX = 1;
this.scaleY = 1;
this.shadow = null;
this.parent.addChild(this);// Bump to top
this.offset = {x:this.x - evt.stageX, y:this.y - evt.stageY};
});
piece.addEventListener(MouseEvent.MOUSE_MOVE, function)
{
this.x = evt.stageX + this.offset.x;
this.y = evt.stageY + this.offset.y;
});
piece.addEventListener(MouseEvent.MOUSE_UP, function)(evt)
{
var target = this.parent["t"+this.name.substr(1)];
if( target && hitTestInRange( target, 30) ){
this.x = target.x;
this.y = target.y;
}
});
}
}
function hitTestInRange( target, range )
{
if( target.x > stage.mouseX - range &&
target.x < stage.mouseX + range &&
target.y > stage.mouseY - range &&
target.y < stage.mouseY + range )
{
return true;
}
return false;
}
Please help me fix this code so I can run my puzzle and move the pieces.
I can upload the flash file if needed

as3 MOUSE_UP stopDrag() not functioning

I am working on a basic as3 slingshot game which uses startDrag() and stopDrag() to let the user pull an object and fire. when the object is not stretching the "elastic" the MOUSE_UP function works as it should, but when it is below the set points and is stretching the string the MOUSE_UP function is not being called.
vars
var gravity = 0.1;
var angle1:Number = 0;
var angle2:Number = 0;
var radius:Number = 1;
var elasticCoefficient:Number = 0.002;
var released:Boolean = true;
var forced:Boolean = false;
var acc:Object = {x:0 , y:0};
var vel:Object = {x:0 , y:0};
var elastic:MovieClip = new MovieClip();
the ENTER_FRAME code is
function doConstantly(e:Event):void
{
acc.x = 0;
acc.y = gravity;
if(released == true)
{
vel.x += acc.x;
vel.y += acc.y;
ball.x += vel.x;
ball.y += vel.y
}
if(ball.y > stage.stageHeight + 500 || ball.y < -50)
{
resetLevel();
}
elastic.graphics.clear();
elastic.graphics.lineStyle(2, 0xFFF2BD);
if(ball.y > point1.y && ball.x < point2.x)
{
forced = true;
var x1:Number = ball.x - point1.x;
var y1:Number = ball.y - point1.y;
var x2:Number = point2.x - ball.x;
var y2:Number = point2.y - ball.y;
var distance1:Number = Math.sqrt(x1 * x1 + y1 * y1);
var distance2:Number = Math.sqrt(x2 * x2 + y2 * y2);
angle1 = Math.atan2(y1,x1);
angle2 = Math.atan2(y2,x2);
var xOffset:Number = Math.cos(angle1 + Math.PI / 2) * radius;
var yOffset:Number = Math.sin(angle1 + Math.PI / 2) * radius;
var xOffset2:Number = Math.cos(angle2 + Math.PI / 2) * radius;
var yOffset2:Number = Math.sin(angle2 + Math.PI / 2) * radius;
angle1 += Math.sin(radius / distance1);
angle2 += Math.sin(radius / distance2) * -1;
elastic.graphics.moveTo(point1.x, point1.y);
elastic.graphics.lineTo(ball.x+xOffset, ball.y+yOffset);
elastic.graphics.moveTo(point2.x, point2.y);
elastic.graphics.lineTo(ball.x+xOffset2, ball.y+yOffset2);
}
else
{
forced = false;
if(forced == true){trace("forced is true")}
if(forced == false){trace("forced is false")}
elastic.graphics.moveTo(point1.x, point1.y);
elastic.graphics.lineTo(point2.x, point2.y);
}
if (released == true && forced == true)
{
acc.x += distance1 * Math.sin(angle2) * elasticCoefficient;
acc.y += - distance1 * Math.cos(angle1) * elasticCoefficient;
acc.x += distance2 * Math.sin(angle1) * elasticCoefficient;
acc.y += - distance2 * Math.cos(angle2) * elasticCoefficient;
vel.x += acc.x;
vel.y += acc.y;
}
}
and the mouse events
function ballMouseDown(event:MouseEvent)
{
//call function to reset level
resetLevel();
//follow mouse
ball.x = mouseX;
ball.y = mouseY;
ball.startDrag();
//set released to false so that gravity wont affect the ball when clicked
released = false;
}
function ballMouseUp(event:MouseEvent)
{
trace("mouse up function called")
released = true; //gravity will affect the ball when released
ball.stopDrag();
}
Thanks.
Try adding the MOUSE_UP handler to the stage instead - at the moment, you will need to release your mouse while it is over the ball which may not be the case.
Update your MOUSE_DOWN handler to attach the listener to the stage:
function ballMouseDown(e:MouseEvent):void
{
// ...your current code.
stage.addEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
}
And removing the listener when the handler is triggered:
function ballMouseUp(e:MouseEvent):void
{
// ...your current code.
stage.removeEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
}

See if children of movieclip on stage

I am creating a platformer with as3 and need to see if children of the movieclip _boundaries are on the stage or not, that way I can remove them and lower the counter so that more will continually generate. So far all I have is below. Please help, been stuck on this for a couple of weeks.
var ObjectArray:Array = [];
var ChildrenColliding:Boolean = false;
var onStageCount:Number = 0;
function generateObjects():void{
if(_vx > 0 && onStageCount < 20){
var Square:MovieClip;
Square = new mcSquare();
Square.x = Math.random() * 1000 + (Math.abs(_boundaries.x) + 50);
Square.y = Math.random() * stage.stageHeight/2.5 + (stage.stageHeight/2.5);
ObjectArray.push(Square);
_boundaries.addChild(Square);
onStageCount += 1;
}
for(var i in ObjectArray){
Square[i] = Square.name;
for(var a in ObjectArray){
if(ObjectArray[i].hitTestObject(ObjectArray[a]) && a != i){ChildrenColliding = true;}
while(ChildrenColliding){
ObjectArray[i].x += (ObjectArray[a].height + 25);
ObjectArray[i].y += (ObjectArray[a].width + 25);
ChildrenColliding = false;
if(ObjectArray[a].hitTestObject(ObjectArray[i]) && a != i){ChildrenColliding = true;}
}
}
}
//CHECK TO SEE IF CHILDREN ARE ON STAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(var w in ObjectArray){
if(_boundaries){
onStageCount -= 1;
trace("removed");
_boundaries.removeChild(ObjectArray[w]);
ObjectArray.splice(w, 1);
}
}
}
You may need to use the localToGlobal method to determine the position of the Square Objects. Something like:
for (var w in ObjectArray) {
if (_boundaries) {
var sq:MovieClip = ObjectArray[w];
var pnt:Point = _boundaries.localToGlobal(new Point(sq.x, sq.y));
if (pnt.x <= 0 || pnt.x >= _boundaries.stage.stageWidth ||
pnt.y <= 0 || pnt.y >= _boundaries.stage.stageHeight) {
// remove square
onStageCount -= 1;
trace("removed");
_boundaries.removeChild(ObjectArray[w]);
ObjectArray.splice(w, 1);
}
}
}
On a side note for general best practice, reserve words starting with capital letters for class names (like MovieClip, Sprite, or MyCustomClass) and use camelCase for variable names. It's helpful when working with other devs to promote best practice.
Hope this helps.
Try this:
//CHECK TO SEE IF CHILDREN ARE ON STAGE!!!!!!!!!!
for(var w in ObjectArray){
if(_boundaries && _boundaries.contains(ObjectArray[w])){
onStageCount -= 1;
trace("removed");
_boundaries.removeChild(ObjectArray[w]);
ObjectArray.splice(w, 1);
}
}