1120: Access of undefined property _stop - actionscript-3

i am having problem in creating a video player in flash via as3, the problem is that whenever i try to compile the project, the compiler error shows:
1120: Access of undefined property _stop.
1120: Access of undefined property _pause.
1120: Access of undefined property _play.
1180: Call to a possibly undefined method Button.
1120: Access of undefined property _prev.
1180: Call to a possibly undefined method Button.
1120: Access of undefined property _next.
And this keeps on going as much i've mentioned these objects, so please help me solve this. and am newbie so a bit hard for me to find the error.
as3:
import flash.events.MouseEvent;
var _xmlLoader :URLLoader = null;
var _urlRequest :URLRequest = null;
var _xml :XML = null;
var _netConn :NetConnection = null;
var _netstr :NetStream = null;
var _video :Video = null;
var _currentVideoId :int = 0;
var _isPlaying :Boolean = false;
var _soundTransform :SoundTransform = new SoundTransform();
var _volume :int = 1;
var _duration :Number = 0;
function Init():void
{
_urlRequest = new URLRequest("vids.xml");
_xmlLoader = new URLLoader();
_xmlLoader = new URLLoader(_urlRequest);
_xmlLoader.addEventListener(Event.COMPLETE, XMLLoaded, false, 0, true);
}
function XMLLoaded($e:Event):void
{
_xml = new XML($e.target.data);
}
function SetupVideo():void
{
_netConn = new NetConnection();
_netConn.addEventListener(NetStatusEvent.NET_STATUS, OnStatusEvent, false, 0, true);
_netConn.connect(null);
}
function OnStatusEvent(stat:Object):void
{
trace(stat.info.code);
switch(stat.info.code)
{
case "NetConnection.Connect.Success":
SetupNetStream();
break;
case "NetStream.Play.Stop":
_stop.enabled = false;
_pause.enabled = false;
_play.enabled = true;
_isPlaying = false;
_netstr.close();
break;
}
}
function SetupNetStream():void
{
_netstr = new NetStream(_netConn);
_netstr.addEventListener(NetStatusEvent.NET_STATUS, OnStatusEvent, false, 0, true);
var $customClient = new Object();
$customClient.onMetaData = onMetaData;
_netstr.client = $customClient
_video = new Video(500, 250);
_video.smoothing = true;
_video.y
_video.x = stage.stageWidth/2 - _video.width/2;
_video.attachNetStream(_netstr);
addChild(_video);
}
function onMetaData($info:Object):void
{
_duration = $info.duration;
}
function SetupButtons():void
{
_prev.addEventListener(MouseEvent.CLICK, PreviousVideo, false, 0, true);
_next.addEventListener(MouseEvent.CLICK,NextVideo,false,0,true);
_play.addEventListener(MouseEvent.CLICK, PlayVideo, false, 0, true);
_pause.addEventListener(MouseEvent.CLICK, PauseVideo, false, 0, true);
_stop.addEventListener(MouseEvent.CLICK, StopVideo, false, 0, true);
_sound.addEventListener(MouseEvent.CLICK, SoundVolume, false, 0, true);
_stop.enabled = false;
_pause.enabled = false;
_prev.enabled = false;
_next.enabled = false;
}
function PreviousVideo($e:MouseEvent):void
{
_currentVideoId -=1;
_stop.enabled = true;
_pause.enabled = true;
_play.enabled = false;
if(_currentVideoId < 0)
{
_currentVideoId = _xml.video.length()-1;
}
_videoName.text = _xml.video[_currentVideoId].#name;
_netstr.play(String(_xml.video[_currentVideoId].#path));
}
function NextVideo($e:MouseEvent):void
{
_currentVideoId +=1;
_stop.enabled = true;
_pause.enabled = true;
_play.enabled = false;
if(_currentVideoId == _xml.video.length())
{
_currentVideoId = 0;
}
_videoName.text = _xml.video[_currentVideoId].#name;
_netstr.play(String(_xml.video[_currentVideoId].#path));
}
function PlayVideo($e:MouseEvent):void
{
_play.enabled= false;
_next.enabled = true;
_prev.enabled = true;
_stop.enabled= true;
_pause.enabled= true;
if(_isPlaying == false)
{
_isPlaying = true;
_netstr.play(String(_xml.video[_currentVideoId].#path));
_videoName.text = _xml.video[_currentVideoId].#name;
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
}else{
_netstr.resume();
}
}
function PauseVideo($e:MouseEvent):void
{
_play.enabled= true;
_pause.enabled= false;
_netstr.pause();
}
function StopVideo($e:MouseEvent):void
{
_stop.enabled= false;
_pause.enabled= false;
_play.enabled= true;
_isPlaying = false;
removeEventListener(Event.ENTER_FRAME, Update);
_netstr.close();
}
function Update($e:Event):void
{
_track.value = (_netstr.time / _duration) * _track.maximum;
}
function SoundVolume($e:MouseEvent):void
{
if( _volume == 1 )
{
_volume = 0;
_sound.label = "Sound On";
}else{
_volume = 1;
_sound.label = "Sound Off";
}
_soundTransform.volume = _volume;
_netstr.soundTransform = _soundTransform;
}
Init();
SetupVideo();
SetupButtons();
And even i've converted this objects to button symbols.

Probably you forgot to assign instance name for the objects, which used as a _stop, _pause, _play buttons etc.
And also check the "Export for the ActionScript" flag in the properties in the library.

Related

Hovering off DPAD sometimes makes character move infinitely in one direction

I have a DPAD in my game when the player holds down let's say the Left DPAD, if he moves his touch to the Up DPAD and let's go, the player continues going in the left direction.
It also works if you hold the a direction, lets say Up continue holding but move off the Up DPAD, sometimes you may continue going in that direction.
What I've tried to prevent this:
On direction clicks, trigger checks on whether your in motion, or
already going a different direction
Setting collisions to force these variables to become false; aka,
you hit something, inMotion = false, etc
That's about all I can think of on how to fix this.
Also, I use a lot of variables for my checks, is it better to change my functions to return booleans, or is this way fine? Just curious.
Game Class
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Game extends MovieClip
{
public var area1:Boolean = true;
public var area2:Boolean = false;
public var area3:Boolean = false;
public var player1:Boolean = true;
public var player2:Boolean = false;
public var upWalkspeed:Number = -5;
public var downWalkspeed:Number = 5;
public var leftWalkspeed:Number = -5;
public var rightWalkspeed:Number = 5;
public var inMotion:Boolean = false;
public var goingUp:Boolean = false;
public var goingDown:Boolean = false;
public var goingLeft:Boolean = false;
public var goingRight:Boolean = false;
public var playerPosKeeper_mc:MovieClip = new mc_PlayerPosKeeper();
public var up_dpad:MovieClip = new dpad_Up();
public var down_dpad:MovieClip = new dpad_Down();
public var left_dpad:MovieClip = new dpad_Left();
public var right_dpad:MovieClip = new dpad_Right();
public var menu_dpad:MovieClip = new dpad_Menu();
public var run_dpad:MovieClip = new dpad_Menu();
public var barrierRoof1_game:MovieClip = new game_BarrierRoof();
public var barrierRoof2_game:MovieClip = new game_BarrierRoof();
public var barrierSide1_game:MovieClip = new game_BarrierSide();
public var barrierSide2_game:MovieClip = new game_BarrierSide();
public var StageCollisions:Array = new Array(barrierRoof1_game, barrierRoof2_game, barrierSide1_game, barrierSide2_game);
// fix MC goes after not before ||| public var player1States:Array = new Array(mc_P1D1,mc_P1D2,"mc_P1L1","mc_P1L2","mc_P1R1","mc_P1R2","mc_P1U1","mc_P1U2");
public function Game()
{
trace("SUCCESS | Constructed Game Class");
var aMove:Movement = new Movement(this);
addChild(aMove);
}
}
}
Movement Class
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TouchEvent;
import flash.net.dns.AAAARecord;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
public class Movement extends MovieClip
{
public function Movement(main:Game)
{
trace("SUCCESS | Constructed Movement Class");
addChild(main.playerPosKeeper_mc);
main.playerPosKeeper_mc.x = 384;
main.playerPosKeeper_mc.y = 46;
addChild(main.up_dpad);
main.up_dpad.x = 55;
main.up_dpad.y = 336;
addChild(main.down_dpad);
main.down_dpad.x = 57;
main.down_dpad.y = 432;
addChild(main.left_dpad);
main.left_dpad.x = 19;
main.left_dpad.y = 372;
addChild(main.right_dpad);
main.right_dpad.x = 118;
main.right_dpad.y = 372;
addChild(main.menu_dpad);
main.menu_dpad.x = 61;
main.menu_dpad.y = 377;
addChild(main.run_dpad);
main.run_dpad.x = 684;
main.run_dpad.y = 369;
addChild(main.barrierRoof1_game);
main.barrierRoof1_game.x = 0;
main.barrierRoof1_game.y = 0;
addChild(main.barrierRoof2_game);
main.barrierRoof2_game.x = 0;
main.barrierRoof2_game.y = 470;
addChild(main.barrierSide1_game);
main.barrierSide1_game.x = 0;
main.barrierSide1_game.y = 0;
addChild(main.barrierSide2_game);
main.barrierSide2_game.x = 790;
main.barrierSide2_game.y = 0;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
main.up_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, upBeginInput);
main.down_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, downBeginInput);
main.left_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, leftBeginInput);
main.right_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, rightBeginInput);
// Maybe add diagnol direction buttons in the future?;
// !! NOTE !!
// Use some sort of value, maybe a return or a variable to sync up animations
// to if the player is moving, in the future
// Movement Directions
// Start of UP Movement
function upBeginInput(e:TouchEvent):void
{
main.inMotion = true;
main.goingUp = true;
main.goingDown = false;
main.goingLeft = false;
main.goingRight = false;
main.up_dpad.addEventListener(TouchEvent.TOUCH_END, upEndInput);
addEventListener(Event.ENTER_FRAME,sendUpMovement);
}
function upEndInput(e:TouchEvent):void
{
main.inMotion = false;
main.goingUp = false;
main.up_dpad.removeEventListener(TouchEvent.TOUCH_END, upEndInput);
removeEventListener(Event.ENTER_FRAME,sendUpMovement);
}
function sendUpMovement():void
{
if (main.inMotion == true && main.goingUp == true && main.goingDown == false && main.goingLeft == false && main.goingRight == false)
{
movePlayer(0, main.upWalkspeed);
}
else
{
}
}
// End of UP Movement
// Start of DOWN Movement
function downBeginInput(e:TouchEvent):void
{
main.inMotion = true;
main.goingUp = false;
main.goingDown = true;
main.goingLeft = false;
main.goingRight = false;
main.down_dpad.addEventListener(TouchEvent.TOUCH_END, downEndInput);
addEventListener(Event.ENTER_FRAME,sendDownMovement);
}
function downEndInput(e:TouchEvent):void
{
main.inMotion = false;
main.goingDown = false;
main.down_dpad.removeEventListener(TouchEvent.TOUCH_END, downEndInput);
removeEventListener(Event.ENTER_FRAME,sendDownMovement);
}
function sendDownMovement():void
{
if (main.inMotion == true && main.goingUp == false && main.goingDown == true && main.goingLeft == false && main.goingRight == false)
{
movePlayer(0, main.downWalkspeed);
}
else
{
}
}
// End of DOWN Movement
// Start of LEFT Movement
function leftBeginInput(e:TouchEvent):void
{
main.inMotion = true;
main.goingUp = false;
main.goingDown = false;
main.goingLeft = true;
main.goingRight = false;
main.left_dpad.addEventListener(TouchEvent.TOUCH_END, leftEndInput);
addEventListener(Event.ENTER_FRAME,sendLeftMovement);
}
function leftEndInput(e:TouchEvent):void
{
main.inMotion = false;
main.goingLeft = false;
main.left_dpad.removeEventListener(TouchEvent.TOUCH_END, leftEndInput);
removeEventListener(Event.ENTER_FRAME,sendLeftMovement);
}
function sendLeftMovement():void
{
if (main.inMotion == true && main.goingUp == false && main.goingDown == false && main.goingLeft == true && main.goingRight == false)
{
movePlayer(main.leftWalkspeed, 0);
}
else
{
}
}
// End of LEFT Movement
// Start of RIGHT Movement
function rightBeginInput(e:TouchEvent):void
{
main.inMotion = true;
main.goingUp = false;
main.goingDown = false;
main.goingLeft = false;
main.goingRight = true;
main.right_dpad.addEventListener(TouchEvent.TOUCH_END, rightEndInput);
addEventListener(Event.ENTER_FRAME,sendRightMovement);
}
function rightEndInput(e:TouchEvent):void
{
main.inMotion = false;
main.goingRight = false;
main.right_dpad.removeEventListener(TouchEvent.TOUCH_END, rightEndInput);
removeEventListener(Event.ENTER_FRAME,sendRightMovement);
}
function sendRightMovement():void
{
if (main.inMotion == true && main.goingUp == false && main.goingDown == false && main.goingLeft == false && main.goingRight == true)
{
movePlayer(main.rightWalkspeed, 0);
}
else
{
}
}
// End of RIGHT Movement
function movePlayer(movementX:Number, movementY:Number):void
{
var originalX:Number = main.playerPosKeeper_mc.x;
var originalY:Number = main.playerPosKeeper_mc.y;
main.playerPosKeeper_mc.x += movementX;
if (checkCollision())
{
main.playerPosKeeper_mc.x = originalX;
}
main.playerPosKeeper_mc.y += movementY;
if (checkCollision())
{
main.playerPosKeeper_mc.y = originalY;
}
}
function checkCollision():Boolean
{
for each (var StageCollisions:MovieClip in main.StageCollisions)
{
if (main.playerPosKeeper_mc.hitTestObject(StageCollisions))
{
return true;
main.inMotion = false;
main.goingUp = false;
main.goingDown = false;
main.goingLeft = false;
main.goingRight = false;
}
}
return false;
}
}
}
}
Before you even start to debug behaviors, you need to understand what algorithmic approach is. You have 4 similar pieces of code which differ in minor details and make your whole script too long and unreadable and hard to manage. Here's the guideline:
var isMoving:Boolean;
var Direction:Point = new Point;
var Buttons:Array = [Up, Down, Left, Right];
// Subscribe all buttons for the same handlers and behaviors.
for each (var aButton:InteractiveObject in Buttons)
{
aButton.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
aButton.addEventListener(MouseEvent.MOUSE_OUT, onUp);
aButton.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onDown(e:MouseEvent):void
{
// Figure which button was pressed.
switch (e.currentTarget)
{
case Up:
Direction.x = 0;
Direction.y = -1;
break;
case Down:
Direction.x = 0;
Direction.y = 1;
break;
case Left:
Direction.x = -1;
Direction.y = 0;
break;
case Up:
Direction.x = 1;
Direction.y = 0;
break;
}
// Now start moving Hero into the Direction.
if (!isMoving)
{
isMoving = true;
addEventListener(Event.ENTER_FRAME, onFrame);
}
}
function onFrame(e:Event):void
{
Hero.x += Direction.x;
Hero.y += Direction.y;
}
function onUp(e:MouseEvent):void
{
// If any of buttons is released or mouse out, stop moving Hero.
removeEventListener(Event.ENTER_FRAME, onFrame);
Direction.x = 0;
Direction.y = 0;
isMoving = false;
}
As you can see, the beauty of algorithmic approach is that you need to handle differently only things that need to be handled differently, in the example case (which is pretty similar to what you want to create) it is the block to set the movement direction. The rest of the code is identical for all the buttons and that's why the whole script is short and readable, easy to understand and manageable.

Implicit coercion of a value of type flash.events:KeyboardEvent to an unrelated type flash.events:MouseEvent

how can I call a function of flash events : MouseEvent in flash events keyboardEvent function
I am a biggner in as3 please help
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
function keyPressed(event:KeyboardEvent){
if (event.keyCode == Keyboard.ENTER)
{
if (keyMode == 0)
{
keyMode = 1;
**startFun(event);**
}
else if (keyMode == 1)
{
**checkfun(event);**
}
else if (keyMode == 2)
{
keyMode = 1;
**anotherPro(event);**
}
}
}
There is several ways, dispatch events, etc. I'll give you a simple example:
// or should be KEY_UP?
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedHandler, false, 0, true);
stage.addEventListener(MouseEvent.CLICK, exampleMouseClickHandler, false, 0, true);
function exampleMouseClickHandler(event:MouseEvent):void
{
//if necessary to pass any parameter, just need to change these mouseClickLogic() / keyPressLogic() methods
mouseClickLogic();
keyPressLogic();
}
function mouseClickLogic():void
{
//do something...
}
function keyPressedHandler(event:KeyboardEvent):void
{
//if necessary to pass any parameter, just need to change these mouseClickLogic() / keyPressLogic() methods
keyPressLogic();
mouseClickLogic();
}
function keyPressLogic():void
{
//to something...
}
You can use like that:
MouseEvent in flash events:
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.CLICK,clickStage);
stage.addEventListener(MouseEvent.MOUSE_UP,upStage);
stage.addEventListener(Event.MOUSE_LEAVE,leaveStage);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownStage);
function clickStage(e:MouseEvent):void
{
trace("click Stage");
}
function upStage(e:MouseEvent):void
{
trace("up Stage");
}
function leaveStage(e:Event):void
{
trace("leave Stage");
}
function mouseDownStage(e:MouseEvent):void
{
trace("mouse Down Stage");
}
keyboardEvent function:
function reportKeyDown(event:KeyboardEvent):void
{
trace("Key Pressed: " + String.fromCharCode(event.charCode) + " (character code: " + event.charCode + ")");
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
I thank all those who tried to help me
this is my program without any error the error which I made before is in this code instead of Event
I typed MousEvent
function startFun(evt:Event):void
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
var elapsedTime:Number;
var NumOfProplem:Number;
var PropSolved:Number;
var completeSet:Boolean;
answerbox.restrict = "0-9";
ProplemBox.restrict = "0-9";
mult2.restrict = "?";
mult1.restrict = "?";
var keyMode:Number = 0;
stage.focus = ProplemBox;
bt1.visible = false;
bt2.visible = false;
//////////////////////////////
btnstart.addEventListener(MouseEvent.CLICK, startFun);
function startFun(evt:Event):void {
if (ProplemBox.text == "") {
stage.focus = ProplemBox;
return;
}
bt1.visible = true;
bt2.visible = true;
NumOfProplem = Number(ProplemBox.text);
PropSolved = 0;
completeSet = false;
elapsedTime = getTimer();
keyMode = 1;
anotherPro(evt);
}
///////////////////////////////
stage.focus = answerbox;
answerbox.setSelection(0,3);
}
/////////////////
bt1.addEventListener(MouseEvent.CLICK,checkfun);
function checkfun(evt:Event):void
{
var x:Number = parseInt(mult1.text);
var y:Number = parseInt(mult2.text);
var z:Number = parseInt(answerbox.text);
if ((x*y )== z){
messagebox.text = "Right";
PropSolved++;
if (PropSolved >= NumOfProplem ){
keyMode = 3;
endGameReport();
}
else{
keyMode = 2;
}
}
else
{
messagebox.text = "Wrong";
stage.focus = answerbox;
answerbox.setSelection(0,3);
}
}
function endGameReport():void{
elapsedTime = (getTimer() - elapsedTime);
stage.focus = ProplemBox;
ProplemBox.setSelection(0,2);
bt1.visible = false;
bt2.visible = false;
messagebox.text = "You completed" + NumOfProplem + "Proplems in"
+ Math.floor(elapsedTime/1000) + "Second";
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
function keyPressed(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.ENTER)
{
if (keyMode == 0)
{
keyMode = 1;
startFun(evt);
}
else if (keyMode == 1)
{
checkfun(evt);
}
else if (keyMode == 2)
{
keyMode = 1;
anotherPro(evt);
}
}
}

How can I use a function parameter to refer to a variable?

Sorry if this question is a bit vague, but this has been driving me nuts recently. It's nothing too complicated, but all I want to do is have the variable 'targetVariable' be affected by a formula. The actual problem lies in the fact that the referenced variable, being 'masterVolume' in this case, is not getting affected by the formula, but rather 'targetVariable' instead. I run the 'makeSlider' function at the bottom of the script. Here's the code:
var masterVolume:Number = 0;
var panning:Number = 0;
function makeSlider(sliderType, X, Y, targetVariable) {
var sliderHandle:MovieClip = new sliderType();
addChild(sliderHandle);
sliderHandle.x = X;
sliderHandle.y = Y;
var dragging:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, updateSlider);
function updateSlider(e:Event):void {
panning = (mouseX/(stage.stageWidth/2))-1;
targetVariable = ((sliderHandle.x-bar.x)/bar.width);
output.text = masterVolume.toString();
if (dragging == true && mouseX >= bar.x && mouseX <= (bar.x + bar.width)) {
sliderHandle.x = mouseX;
}
}
sliderHandle.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(e:MouseEvent):void {
dragging = true;
}
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function endDrag(e:MouseEvent):void {
dragging = false;
}
}
function playSound(target, intensity:Number, pan:Number) {
var sound:Sound = new target();
var transformer:SoundTransform = new SoundTransform(intensity, pan);
var channel:SoundChannel = new SoundChannel();
channel=sound.play();
channel.soundTransform = transformer;
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, make);
function make(e:MouseEvent):void {
playSound(test, masterVolume, panning);
}
makeSlider(SliderHandle, bar.x, bar.y, masterVolume);
Okay, so I studied the Object class and found out that I could reference the variable by making it an object in the function. Here's the updated, working script:
var panning:Number = 0;
var masterVolume:Number = 0;
function makeSlider(sliderType, barType, soundType, hitBoxScale:Number, X, Y, targetVariable) {
var reference:Object = { targetVariable: 0 };
var slider:MovieClip = new sliderType;
var newBar:MovieClip = new barType;
addChild(newBar);
newBar.x = X;
newBar.y = Y;
addChild(slider);
slider.x = X;
slider.y = Y;
var dragging:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, updateSlider);
function updateSlider(e:Event):void {
panning = (mouseX/(stage.stageWidth/2))-1;
reference.targetVariable = (slider.x-newBar.x)/newBar.width;
if (dragging == true && mouseX >= newBar.x && mouseX <= (newBar.x + newBar.width)) {
slider.x = mouseX;
}
if (reference.targetVariable <= 0.01) {
output.text = ("None");
}
if (reference.targetVariable >= 0.99) {
output.text = ("Max");
}
if (reference.targetVariable > 0.01 && reference.targetVariable < 0.99) {
output.text = (Math.round((reference.targetVariable*100))+"%").toString();
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(e:MouseEvent):void {
if (mouseY >= newBar.y-hitBoxScale && mouseY <= (newBar.y + newBar.height)+hitBoxScale) {
dragging = true;
}
}
slider.addEventListener(MouseEvent.MOUSE_DOWN, beginDragFromSlider);
function beginDragFromSlider(e:MouseEvent):void {
dragging = true;
}
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function endDrag(e:MouseEvent):void {
if (dragging == true) {
playSound(soundType, reference.targetVariable, 0);
}
dragging = false;
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, make);
function make(e:MouseEvent):void {
if (dragging == false) {
playSound(test, reference.targetVariable, panning);
}
}
}
function playSound(target, intensity:Number, pan:Number) {
var sound:Sound = new target();
var transformer:SoundTransform = new SoundTransform(intensity, pan);
var channel:SoundChannel = new SoundChannel();
channel=sound.play();
channel.soundTransform = transformer;
}
makeSlider(defaultSlider, defaultBar, volumeIndicator, 10, 134, 211, masterVolume);

as3 child as movieclip - won't accept functions

this follows on from my last question which I thought was answered but for some reason when I treat a child of my stage (display object) as a movieclip I can't then apply the usual functions that I want to:
var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc1) {
mc1.useHandCursor = true;
mc1.buttonMode = true;
mc1.addEventListener(MouseEvent.CLICK, fillDriveway);
}
Any wisdom would greatly appreciated... and sorry for asking such a similar question as previous...
Thanks in advance.
EDIT: More code from the AS on this project for context:
import flash.display.*
ImageUploader.visible = false;
function showUploader(e:MouseEvent):void {
ImageUploader.visible = true;
ImageUploader.gotoAndPlay(2);
}
pic.addEventListener(MouseEvent.CLICK,addNewPoint);
var n:Number = 0;
var joinPointsArray:Array = new Array;
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
if(n==1){
var nextPoint:MovieClip = new mcstart();
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
}else{
var nextPoint2:MovieClip = new newPoint();
addChild(nextPoint2);
nextPoint2.name = "mc"+pointNo.text;
nextPoint2.x = e.target.mouseX;
nextPoint2.y = e.target.mouseY;
}
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPointsArray.push(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var i:int=2; i<=n; ++i){
joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
}
}
pic.addEventListener(MouseEvent.CLICK, addNewPoint);
function fillDriveway(eventObject:MouseEvent) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
for(var p:int=0; p<(joinPointsArray.length); ++p) {
joinPointsArray[p].alpha = 0;
}
this.getChildByName("mc1").alpha = 0;
joinPoints.graphics.beginFill(0xFFFFFF, 0.7);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var m:int=2; m<=n; ++m){
joinPoints.graphics.lineTo(this.getChildByName("mc"+m).x, this.getChildByName("mc"+m).y);
}
joinPoints.name = "driveshape";
filledDrive.text = "filled";
}
function undoit(eventObject:MouseEvent) {
if(n > 0) {
if(filledDrive.text.indexOf("filled") != -1) {
this.removeChild(this.getChildAt(this.numChildren -1));
filledDrive.text = "";
}else{
this.removeChild(this.getChildAt(this.numChildren -1));
this.removeChild(this.getChildAt(this.numChildren -1));
n--;
pointNo.text = String(n);
}
}
}
function maskDrive(eventObject:MouseEvent) {
if(filledDrive.text.indexOf("filled") != -1) {
var finishA:MovieClip = new finishMC();
this.addChild(finishA);
finishA.x = 310;
finishA.y = 100;
finishA.mask = getChildByName("driveshape");
finishA.gotoAndPlay(2);
}
}
//BTN RollOvers
function btn1over(myEvent:MouseEvent) {
btn1.gotoAndPlay(2);
}
function btn1out(myEvent:MouseEvent) {
btn1.gotoAndPlay(11);
}
function btn2over(myEvent:MouseEvent) {
btn2.gotoAndPlay(2);
}
function btn2out(myEvent:MouseEvent) {
btn2.gotoAndPlay(11);
}
function btn3over(myEvent:MouseEvent) {
btn3.gotoAndPlay(2);
}
function btn3out(myEvent:MouseEvent) {
btn3.gotoAndPlay(11);
}
function undoover(myEvent:MouseEvent) {
undo.gotoAndPlay(2);
}
function undoout(myEvent:MouseEvent) {
undo.gotoAndPlay(11);
}
//BTN Calls
btn1HIT.addEventListener(MouseEvent.CLICK, fillDriveway);
btn1HIT.addEventListener(MouseEvent.ROLL_OVER, btn1over);
btn1HIT.addEventListener(MouseEvent.ROLL_OUT, btn1out);
btn1HIT.buttonMode = true;
btn1HIT.useHandCursor = true;
btn2HIT.addEventListener(MouseEvent.CLICK, maskDrive);
btn2HIT.addEventListener(MouseEvent.ROLL_OVER, btn2over);
btn2HIT.addEventListener(MouseEvent.ROLL_OUT, btn2out);
btn2HIT.buttonMode = true;
btn2HIT.useHandCursor = true;
btn3HIT.buttonMode = true;
btn3HIT.useHandCursor = true;
btn3HIT.addEventListener(MouseEvent.ROLL_OVER, btn3over);
btn3HIT.addEventListener(MouseEvent.ROLL_OUT, btn3out);
btn3HIT.addEventListener(MouseEvent.CLICK, showUploader);
undoHIT.addEventListener(MouseEvent.CLICK, undoit);
undoHIT.addEventListener(MouseEvent.ROLL_OVER, undoover);
undoHIT.addEventListener(MouseEvent.ROLL_OUT, undoout);
undoHIT.buttonMode = true;
undoHIT.useHandCursor = true;
var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc1) {
mc1.useHandCursor = true;
mc1.buttonMode = true;
mc1.addEventListener(MouseEvent.CLICK, fillDriveway);
}
Are you sure the movieclip is placed on stage or actually converted to movieclip?
Try stage.getChildByName(). Where did you place this code? Inside a frame or inside a main document class? To be sure you can check the childs are added on stage and see what their names are.
You can use this code
for ( var i :int = 0; i < this.numChildren; i++ )
{
babe = this.getChildAt( i );
if ( babe is MovieClip) {
trace( babe.name);
}
}
I've also seen this, not sure if it works.
if (stage.contains(mc1)) {
}
Wahoo! Figured it out!
By popping
var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
at the end of my AS I was referring to the child "mc1" before it existed - it isn't created until the user clicks somewhere on the "pic" movieclip. So the solution was to bung my actions for "mc1" (including declaring it as a MovieClip) into a separate function:
function createstartEndMC():void {
var startEnd:MovieClip = (this.getChildByName("mc1")) as MovieClip;
startEnd.useHandCursor = true;
startEnd.buttonMode = true;
startEnd.addEventListener(MouseEvent.CLICK, fillDriveway);
}
and then to call this function AFTER "mc1" child is created:
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
if(n==1){
var nextPoint:MovieClip = new mcstart();
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
createstartEndMC();
}
Finally it works and "mc1" (or "startEnd" as I call it once it's created and made into an MC) finally behaves as a normal timeline MC should!
I'm so happy - thanks for all your guidance!
Cam

AS3 Showing image after load is finish

So i'm loading picture from xml, and adding them into a movieclip called cv which has a holder called cHolder. Right now the problem is that while the preloader shows it is loading, the cv(s) appeared already. Is there anyway to show all the cv only after the images have finish loading?
Thanks.
for each (var projectName:XML in projectAttributes)
{
//trace(projectName);
var projectDP:XMLList = projectInput.project.(#name == projectName).displayP;
//trace(projectDP);
var cv:MovieClip = new cView();
catNo += 1;
cv.name = "cv" + catNo;
cv.buttonMode = true;
if(catNo % 5 == 0)
{
catY += 137;
catX = -170;
cv.x = catX;
cv.y = catY;
}
else
{
cv.x = catX;
cv.y = catY;
catX += 112;
}
var imageLoader = new Loader();
imageLoader.load(new URLRequest(projectDP));
TweenLite.to(cv.cHolder, 1, {colorTransform:{tint:0x000000, tintAmount:0.8}});
cv.cHolder.addChild(imageLoader);
cv.ct.text = projectName;
projName.push(projectName);
this.addChild(cv);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProg);
function imageProg(e:ProgressEvent):void
{
loader.visible = true;
var imageLoaded:Number = e.bytesLoaded/e.bytesTotal*100;
loader.scaleX = imageLoaded/100;
}
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
function imageLoad(e:Event):void
{
loader.visible = false;
}
First, don't put a function inside another function, this won't help for anything and is a bad habit :)
Declare two private variables:
var nImages:uint;
var loadedImages:uint;
Before the loop:
nImages = projectAttributes.length();
loadedImages = 0;
cv.visible = false;
and in the Event.COMPLETE listener:
function imageLoad(e:Event):void
{
loader.visible = false;
loadedImages++;
if (loadedImages == nImages)
{
cv.visible = true;
}
}
var count:int = 0;
var totalClips:int = 0;
cv.visible = false;
for each (var projectName:XML in projectAttributes)
{
++totalClips;
//trace(projectName);
var projectDP:XMLList = projectInput.project.(#name == projectName).displayP;
//trace(projectDP);
var cv:MovieClip = new cView();
catNo += 1;
cv.name = "cv" + catNo;
cv.buttonMode = true;
if(catNo % 5 == 0)
{
catY += 137;
catX = -170;
cv.x = catX;
cv.y = catY;
}
else
{
cv.x = catX;
cv.y = catY;
catX += 112;
}
var imageLoader = new Loader();
imageLoader.load(new URLRequest(projectDP));
TweenLite.to(cv.cHolder, 1, {colorTransform:{tint:0x000000, tintAmount:0.8}});
cv.cHolder.addChild(imageLoader);
cv.ct.text = projectName;
projName.push(projectName);
this.addChild(cv);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProg);
function imageProg(e:ProgressEvent):void
{
loader.visible = true;
var imageLoaded:Number = e.bytesLoaded/e.bytesTotal*100;
loader.scaleX = imageLoaded/100;
}
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoad);
function imageLoad(e:Event):void
{
++count;
if(count == totalClips){
cv.visible = true;
}
loader.visible = false;
}
}
So you might want to adapt things a little, in case I'm counting in the wrong spots etc for your implementation but as you can see the solution is simple, you count the total number of clips being processed for loading from the XML, then as the images are loaded and call the OnComplete callback, you increment another count until you've seen that you've loaded all processed images, and set the container to visible.