Copy link to clipboard
Copied
I am creating a game wherein there are 4 buttons. The 4 buttons are baskets with labels of solid, liquid, gas and fire. When the button is clicked, the basket that the catcher/character is holding will change depending on what button is clicked. This time I have connected this part of the game from the other parts. My problem is that everytime I load this part of the game the buttons cannot change the basket my catcher is holding, it cannot change frames. But if it is individually played it is working quite well. Why is that?
it's all finished. i just copied the same frames to all of my catcher objects from the other stages.
Copy link to clipboard
Copied
//Code for moving through the frames of my movieclip(catcher)
public function fl_MouseClickHandler(event:MouseEvent):void
{
catcher.gotoAndPlay(41);
soundfx_4.play();
}
public function fl_MouseClickHandler_1(event:MouseEvent):void
{
catcher.gotoAndPlay(81);
soundfx_4.play();
}
public function fl_MouseClickHandler_2(event:MouseEvent):void
{
catcher.gotoAndPlay(121);
soundfx_4.play();
}
public function fl_MouseClickHandler_3(event:MouseEvent):void
{
catcher.gotoAndPlay(161);
soundfx_4.play();
}
Copy link to clipboard
Copied
Where are the buttons that are being clicked relative to the catcher that you are trying to target. Your title makes it sound like that code is in a different file than the one with the catcher.
Copy link to clipboard
Copied
it's in the same file as the catcher, sorry for the confusing title.
Copy link to clipboard
Copied
If it is not a separate file, then what do you mean when you say you load it? Where is that code relative to the timeline where the catcher is?
Put traces in the event handler functions you show so that you can confirm whether or not the buttons are working with them.
Copy link to clipboard
Copied
the loaded swf file(stage4 - where i intend to change frames within my movieclip (catcher)) will be coming from another swf file(stage3). what code are you talking about?
yes it works because i have put a trace on it and it is tracing the words i put correctly and the sounds play everytime i click on the button.
Copy link to clipboard
Copied
sorry but i did not get what you mean in your 2nd question, so i'll just post the whole code for this file. by the way it is in an .as file.
package
{
import flash.automation.StageCaptureEvent;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.display.Stage;
import flash.utils.getDefinitionByName;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
public class Stage4 extends MovieClip {
var myloader:Loader = new Loader();
var catcher:Catcher;
var nextObject:Timer;
var objects:Array = new Array();
var ObjectHalfWidth: Number;
const speed: Number = 10.0;
var score: int = 0;
var fl_TimerInstance:Timer = new Timer(1000, 60);
var fl_SecondsElapsed:Number = 60;
var music_1:Sound = new Sound(new URLRequest("Cheerful_Theme.mp3"));
var music_2:Sound = new Sound(new URLRequest("Congrats_Theme.mp3"));
var music_3:Sound = new Sound(new URLRequest("Game_Over.mp3"));
var soundfx_1:Sound = new Sound(new URLRequest("Catch_Good.mp3"));
var soundfx_2:Sound = new Sound(new URLRequest("Catch_Bad.mp3"));
var soundfx_3:Sound = new Sound(new URLRequest("Ticking_Clock.mp3"));
var soundfx_4:Sound = new Sound(new URLRequest("Button_Click.mp3"));
var boundary:falling_boundary;
var banner: Banner_down;
var s_btn: Solid_btn;
var l_btn: Liquid_btn;
var g_btn: Gas_btn;
var f_btn: Fire_btn;
public function Stage4 () {
catcher = new Catcher ();
boundary = new falling_boundary();
banner = new Banner_down;
catcher.y = 720;
catcher.x = 120;
addChild(catcher);
addChild(boundary);
addChild(banner);
banner.x = 512.1;
banner.y = 768;
boundary.x = 512.1;
boundary.y = 690;
setChildIndex(banner, numChildren -1);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();
music_1.play();
s_btn = new Solid_btn();
addChild(s_btn);
s_btn.y = 720;
s_btn.x = 90.45;
l_btn = new Liquid_btn();
addChild(l_btn);
l_btn.y = 1047;
l_btn.x = 280.45;
g_btn = new Gas_btn();
addChild(g_btn);
g_btn.y = 720
g_btn.x = 433.45;
f_btn = new Fire_btn();
addChild(f_btn);
f_btn.y = 828.70;
f_btn.x = 603.45;
s_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
l_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_1);
g_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
f_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
setNextObject();
addEventListener(Event.ENTER_FRAME, moveObjects);
}
public function setNextObject() {
nextObject = new Timer(1000 + Math.random() * 1000, 1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);
nextObject.start();
if (fl_SecondsElapsed == 0)
{
nextObject.stop();
}
}
public function newObject(e:TimerEvent) {
var solidObjects:Array = ["Chocolate","Apple", "Laptop", "USB"];
var liquidObjects:Array = ["Coffee", "CookingOil", "Milk", "Water"];
var gasObjects:Array = ["Argon", "Carbon_Dioxide", "Helium", "Water_Vapor"];
var badObjects:Array = ["Fire"];
var randomNum:Number = Math.random();
if ((randomNum >= .1) && (randomNum <= .3)) {
var r:int = Math.floor(Math.random() * solidObjects.length);
var classRef:Class = getDefinitionByName(solidObjects
var newObject:MovieClip = new classRef();
newObject.typestr = "solid";
}
else if ((randomNum >= .4) && (randomNum <=.6)) {
r = Math.floor(Math.random() * liquidObjects.length);
classRef = getDefinitionByName(liquidObjects
newObject = new classRef ();
newObject.typestr = "liquid";
}
else if ((randomNum >= .7) && (randomNum <= 9)) {
r = Math.floor(Math.random() * gasObjects.length);
classRef = getDefinitionByName(gasObjects
newObject = new classRef ();
newObject.typestr = "gas";
}
else {
r = Math.floor(Math.random() * badObjects.length);
classRef = getDefinitionByName(badObjects
newObject = new classRef ();
newObject.typestr = "bad";
}
newObject.y = -300;
newObject.x = 1024;
newObject.x = (Math.random() * 400) + (Math.random()*400);
addChild(newObject);
objects.push(newObject);
setNextObject();
}
public function moveObjects(e:Event) {
for (var i: int = objects.length -1; i >= 0; i--) {
objects.y += speed;
addChild(objects);
if (objects.hitTestObject(boundary)) {
score -= 5;
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
else if(catcher["mouth_mc_"+2]){
if (objects.hitTestObject(catcher.mouth_mc_2)) {
if (objects.typestr == "solid") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_2.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_2.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
}
else if(catcher["mouth_mc_"+3]){
if (objects.hitTestObject(catcher.mouth_mc_3)) {
if (objects.typestr == "liquid") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_3.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_3.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
}
else if(catcher["mouth_mc_"+4]){
if (objects.hitTestObject(catcher.mouth_mc_4)) {
if (objects.typestr == "gas") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_4.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_4.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
}
else if(catcher["mouth_mc_"+5]){
if (objects.hitTestObject(catcher.mouth_mc_5)) {
if (objects.typestr == "bad") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_5.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_5.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
else if (objects.hitTestObject(boundary)) {
removeChild(objects);
objects.splice(i, 1);
}
}
catcher.x = mouseX;
checkStageBorder();
}
}
public function fl_TimerHandler(Event:TimerEvent):void
{
fl_SecondsElapsed--;
TimerDisplay.text = String(fl_SecondsElapsed);
if(fl_SecondsElapsed == 10)
{
soundfx_3.play();
}
if (fl_SecondsElapsed == 0 && score >= 60)
{
stopGame();
gotoAndPlay(1, "Scene 2");
SoundMixer.stopAll();
music_2.play();
}
else if (fl_SecondsElapsed == 0 && score < 60)
{
stopGame();
gotoAndPlay(1, "Scene 3");
SoundMixer.stopAll();
music_3.play();
}
}
public function checkStageBorder()
{
ObjectHalfWidth = catcher.width / 2;
if (catcher.x - ObjectHalfWidth < 0)
{
catcher.x = 18 + ObjectHalfWidth;
}
else if (catcher.x + ObjectHalfWidth > stage.stageWidth)
{
catcher.x = stage.stageWidth - ObjectHalfWidth + 17;
}
}
public function stopGame()
{
for (var i:int = 0; i < objects.length; i++)
{
this.removeChild(objects);
objects.splice(i, 1);
nextObject.stop();
}
this.removeChild(catcher);
}
public function fl_MouseClickHandler(event:MouseEvent):void
{
catcher.gotoAndPlay(41);
soundfx_4.play();
}
public function fl_MouseClickHandler_1(event:MouseEvent):void
{
catcher.gotoAndPlay(81);
soundfx_4.play();
}
public function fl_MouseClickHandler_2(event:MouseEvent):void
{
catcher.gotoAndPlay(121);
soundfx_4.play();
}
public function fl_MouseClickHandler_3(event:MouseEvent):void
{
catcher.gotoAndPlay(161);
soundfx_4.play();
}
}
}
Copy link to clipboard
Copied
Put traces in the event handler functions you show so that you can confirm whether or not the buttons are working with them.
Copy link to clipboard
Copied
yes the buttons are working fine.
public function fl_MouseClickHandler(event:MouseEvent):void
{
trace('solid')
catcher.gotoAndPlay(41);
soundfx_4.play();
}
in here i have put a trace the output showed the word 'solid' and the soundfx_4 played fine.
the problem is i cannot access the 41st frame and any other frames in my catcher movieclip.
Copy link to clipboard
Copied
In your catcher object, try adding traces to the frames you say to go to (41, 81, 121, 161) just to see if you actually get there or not. Maybe something is causing them to go and return before you get a chance to see it happen.
Copy link to clipboard
Copied
when i put a trace on those frames nothing is happening on the output window. those frames are not being accessed, what should i do to fix it?
Copy link to clipboard
Copied
it's all finished. i just copied the same frames to all of my catcher objects from the other stages.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now