• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Puzzle game in adobe animate

Community Beginner ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

Hello! i am making a puzzle game for school that is due today. I need to make a three level puzzle game. Each level has 12 objects, 6 objects being the puzzles themselfs and the other 6 drop zones. The code that is below only works for one level. I changed the item names and put the code into the second frame. The game doesnt let the player click on the puzzles and im not sure where the problem lays.

import flash.events.MouseEvent;
import flash.events.Event;
stop();
var count:Number=0;
stage.addEventListener (Event.ENTER_FRAME,PuzzleLoop);
function PuzzleLoop(e:Event):void
{

if (count >=6)
{
stage.removeEventListener(Event.ENTER_FRAME, PuzzleLoop);
item1_mc.visible= false;
item2_mc.visible= false;
item3_mc.visible= false;
item4_mc.visible= false;
item5_mc.visible= false;
item5_mc.visible= false;
gotoAndStop(2);
}

}
var orig1X:Number=item1_mc.x;
var orig1Y:Number=item1_mc.y;
var orig2X:Number=item2_mc.x;
var orig2Y:Number=item2_mc.y;
var orig3X:Number=item3_mc.x;
var orig3Y:Number=item3_mc.y;
var orig4X:Number=item4_mc.x;
var orig4Y:Number=item4_mc.y;
var orig5X:Number=item5_mc.x;
var orig5Y:Number=item5_mc.y;
var orig6X:Number=item6_mc.x;
var orig6Y:Number=item6_mc.y;
item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release);
item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);
item4_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item4_mc.addEventListener(MouseEvent.MOUSE_UP, item4Release);
item5_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item5_mc.addEventListener(MouseEvent.MOUSE_UP, item5Release);
item6_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item6_mc.addEventListener(MouseEvent.MOUSE_UP, item6Release);
item1_mc.buttonMode=true;
item2_mc.buttonMode=true;
item3_mc.buttonMode=true;
item4_mc.buttonMode=true;
item5_mc.buttonMode=true;
item6_mc.buttonMode=true;
function dragTheObject(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-1;
this.setChildIndex(item, topPos);
}

function item1Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone1_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone1_mc.x;
item.y=dropZone1_mc.y;
item1_mc.buttonMode=false;
item1_mc.removeEventListener( MouseEvent.MOUSE_DOWN, dragTheObject );

count++;
}
else {
item.x=orig1X;
item.y=orig1Y;
}}

function item2Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone2_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone2_mc.x;
item.y=dropZone2_mc.y;
item2_mc.buttonMode=false;
item2_mc.removeEventListener( MouseEvent.MOUSE_DOWN, dragTheObject );
count++;
}
else {
item.x=orig2X;
item.y=orig2Y;
}}

function item3Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone3_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone3_mc.x;
item.y=dropZone3_mc.y;
item3_mc.buttonMode=false;
item3_mc.removeEventListener( MouseEvent.MOUSE_DOWN, dragTheObject );
count++;
} else {
item.x=orig3X;
item.y=orig3Y;
}}

function item4Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone4_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone4_mc.x;
item.y=dropZone4_mc.y;
item4_mc.buttonMode=false;
item4_mc.removeEventListener( MouseEvent.MOUSE_DOWN, dragTheObject );
count++;
}
else {
item.x=orig4X;
item.y=orig4Y;
}}

function item5Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone5_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone5_mc.x;
item.y=dropZone5_mc.y;
item5_mc.buttonMode=false;
item5_mc.removeEventListener( MouseEvent.MOUSE_DOWN, dragTheObject );
count++;
} else {
item.x=orig5X;
item.y=orig5Y;
}}

function item6Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone6_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone6_mc.x;
item.y=dropZone6_mc.y;
item6_mc.buttonMode=false;
item6_mc.removeEventListener( MouseEvent.MOUSE_DOWN, dragTheObject );
count++;
}
else {
item.x=orig6X;
item.y=orig6Y;
}}

function checkAnswers(event:MouseEvent):void {
if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
dropZone2_mc.hitTestPoint(item2_mc.x,item2_mc.y) &&
dropZone3_mc.hitTestPoint(item3_mc.x,item3_mc.y) &&
dropZone4_mc.hitTestPoint(item4_mc.x,item4_mc.y) &&
dropZone5_mc.hitTestPoint(item5_mc.x,item5_mc.y) &&
dropZone6_mc.hitTestPoint(item6_mc.x,item6_mc.y)) {

if (count >=3)
{
gotoAndStop(2);
}
} else {

}}


function itemRelease(event:MouseEvent):void {
var thisItem:MovieClip = MovieClip(event.target);
thisItem.stopDrag();
if (dropZone1_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone1_mc.x;
thisItem.y = dropZone1_mc.y;
}
else if (dropZone2_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone2_mc.x;
thisItem.y = dropZone2_mc.y;
}
else if (dropZone3_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone3_mc.x;
thisItem.y = dropZone3_mc.y;
}
else if (dropZone4_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone4_mc.x;
thisItem.y = dropZone4_mc.y;
}
else if (dropZone5_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone5_mc.x;
thisItem.y = dropZone5_mc.y;
}
else if (dropZone6_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone6_mc.x;
thisItem.y = dropZone6_mc.y;
}
else if (thisItem==item1_mc) {
event.target.x = orig1X;
event.target.y = orig1Y;
}
else if (thisItem==item2_mc) {
event.target.x = orig2X;
event.target.y = orig2Y;
}
else if (thisItem==item3_mc) {
event.target.x = orig3X;
event.target.y = orig3Y;
}
else if (thisItem==item4_mc) {
event.target.x = orig4X;
event.target.y = orig4Y;
}
else if (thisItem==item5_mc) {
event.target.x = orig5X;
event.target.y = orig5Y;
}
else {
event.target.x = orig6X;
event.target.y = orig6Y;
}}

TOPICS
ActionScript , Code , Error

Views

233

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 19, 2024 Apr 19, 2024

Hi.

 

I think some encapsulation and other object oriented programming approaches may help a little bit. For example, using containers in different frames and also custom properties on the items instead of variables.

 

Here is a suggestion in which I tried to make your code more dynamic and less DRY but not too much complex.

 

AS3 code:
[Frame 1]

import flash.display.MovieClip;
import flash.events.Event;

var matches:uint;
var totalMatches:uint;

function game1():void
{
	matches = 0;
	totalMatche
...

Votes

Translate

Translate
Community Expert ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

Hi.

 

I think some encapsulation and other object oriented programming approaches may help a little bit. For example, using containers in different frames and also custom properties on the items instead of variables.

 

Here is a suggestion in which I tried to make your code more dynamic and less DRY but not too much complex.

 

AS3 code:
[Frame 1]

import flash.display.MovieClip;
import flash.events.Event;

var matches:uint;
var totalMatches:uint;

function game1():void
{
	matches = 0;
	totalMatches = 0;
	
	setPiece(board1.item1_mc, board1.dropZone1_mc);
	setPiece(board1.item2_mc, board1.dropZone2_mc);
	setPiece(board1.item3_mc, board1.dropZone3_mc);
	setPiece(board1.item4_mc, board1.dropZone4_mc);
	setPiece(board1.item5_mc, board1.dropZone5_mc);
	setPiece(board1.item6_mc, board1.dropZone6_mc);
	
	stop();
}

function setPiece(target:MovieClip, dropZone:MovieClip):void
{
	target.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
	target.addEventListener(MouseEvent.MOUSE_UP, releaseHandler);
	target.buttonMode = true;
	target.dropZone = dropZone;
	target.originX = target.x;
	target.originY = target.y;
	totalMatches++;
}

function dragHandler(event:MouseEvent):void
{
	var item:MovieClip = MovieClip(event.target);
	
	item.startDrag();
	item.parent.addChild(item);
}

function releaseHandler(event:MouseEvent):void
{
	var item:MovieClip = MovieClip(event.target);
	
	item.stopDrag();
	
	if (item.hitTestPoint(item.dropZone.x, item.dropZone.y))
	{
		item.x = item.dropZone.x;
		item.y = item.dropZone.y;
		item.buttonMode = false;
		item.removeEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
		item.removeEventListener(MouseEvent.MOUSE_UP, releaseHandler);
		matches++;
		
		if (currentFrame < totalFrames && matches == totalMatches)
			nextFrame();
	}
	else
	{
		item.x = item.originX;
		item.y = item.originY;
	}
}

game1();


[Frame 2]

function game2():void
{
	matches = 0;
	totalMatches = 0;
	
	setPiece(board2.item1_mc, board2.dropZone1_mc);
	setPiece(board2.item2_mc, board2.dropZone2_mc);
	setPiece(board2.item3_mc, board2.dropZone3_mc);
	setPiece(board2.item4_mc, board2.dropZone4_mc);
	setPiece(board2.item5_mc, board2.dropZone5_mc);
	setPiece(board2.item6_mc, board2.dropZone6_mc);	
}

game2();


[Frame 3]

function game3():void
{
	matches = 0;
	totalMatches = 0;
	
	setPiece(board3.item1_mc, board3.dropZone1_mc);
	setPiece(board3.item2_mc, board3.dropZone2_mc);
	setPiece(board3.item3_mc, board3.dropZone3_mc);
	setPiece(board3.item4_mc, board3.dropZone4_mc);
	setPiece(board3.item5_mc, board3.dropZone5_mc);
	setPiece(board3.item6_mc, board3.dropZone6_mc);
}

game3();


Download / FLA / source:
https://bit.ly/4d6EZ0a

I hope this helps.

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

Thank you for the reply but the code still doesnt work. I attached the error message.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

Have you downloaded the FLA?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

I have now, sorry Im new to adobe animate and didnt realize. Everything works, Thank you so much for the help, Have a good day.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

You're welcome!

 

Please let us know if you need further assistance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

Ive encountered a problem while working with the FLA file. Following the example game provided, I have selected all the elements and created a symbol  "Board" and named it as "board1," (i did the same for the other two levels)

However, I'm facing a challenge where the puzzle pieces dont want to remain in the designated dropzone once I attempt to place them there.Also ,Given that the code specifically references only "item1_mc" through "item6_mc," I'm curious if it's necessary to add the other items in the code for each level. (Each level features  geometric shapes labeled from "item1_mc" to "item36_mc," mirroring the arrangement of dropzones.). I am very sorry to be bothering you once more.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2024 Apr 19, 2024

Copy link to clipboard

Copied

Do you have a FLA for us to look into?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 20, 2024 Apr 20, 2024

Copy link to clipboard

Copied

LATEST

I sent you the link to the fla file on messages. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines