1180: Call to a possibly undefined method FlipUntilHead - actionscript-3

package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.MouseEvent;
public class coinFlip extends MovieClip {// class
var bool:Boolean;
var num1:Number;
var counter:int;
public function coinFlip() {// constructor
RandomTruth();
var textF1:TextField = new TextField();
FlipUntilHead(RandomTruth); // the error is right here*
if (bool == false) {
textF1.text = "tails";
} else {
textF1.text = "heads";
}
addChild(textF1);
function RandomTruth() {
num1 = Math.random();
//trace(num1);
if (num1 < 0.5) {
bool = false;
return;
} else {
bool = true;
return;
}
function FlipUntilHead(RandomTruth) {
while (bool == false) {
RandomTruth();
counter ++;
return
}
//trace(counter);
}
}
}// end of constructor
}// end of class
}// end of package (program)
The program is supposed to "flip" a coin until it lands on heads using Math.random and boolean variables to declare if its "heads" or "tails".
I think it might be a very simple error and I am just dumb (beginner in programming).
The error is:
"1180: Call to a possibly undefined method FlipUntilHead"

Problem 1: You do not have to pass any parameters into your FlipUntilHead() function, because that function already calls RandomTruth().
Instead of function FlipUntilHead(RandomTruth) use function FlipUntilHead().
Then, instead of
RandomTruth();
var textF1:TextField = new TextField();
FlipUntilHead(RandomTruth);
use
var textF1:TextField = new TextField();
FlipUntilHead();
Although the function name indeed existed, the error was being thrown because it didn't understand the way that you were passing RandomTruth into it.
Problem 2: Also, there should be a } just before function FlipUntilHead(){, and one less } at the end of your program.
Problem 3: Finally, the while loop within FlipUntilHead() is not looping properly, because there is a return statement that immediately ends the function at the first loop. Here is the final code:
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.MouseEvent;
public class coinFlip extends MovieClip {
var bool:Boolean;
var num1:Number;
var counter:int;
public function coinFlip() {
var textF1:TextField = new TextField();
FlipUntilHead();
if (bool == false) {
textF1.text = "tails";
} else {
textF1.text = "heads";
}
addChild(textF1);
function RandomTruth() {
num1 = Math.random();
if (num1 < 0.5) {
bool = false;
return;
} else {
bool = true;
return;
}
}
function FlipUntilHead() {
while (bool == false) {
RandomTruth();
counter++;
}
}
}
}
}

Related

How do I get my movieclip character to move?

I've been trying for a few hours now and I cant get my little character to move with the keyboard.
I have ran a trace to make see if anything was happening and the position value does change but my character doesn't react to that position change.
I receive no errors. Both my Character and BrickBlock are movieclips and they have been imported for ActionScript.
If any other information is needed please let me know. Thank you! :)
My following code:
package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class CharMove extends MovieClip {
var char1 :Character;
var block :BrickBlock;
public function CharMove()
{
char1 = new Character();
block = new BrickBlock();
//this.addEventListener(Event.ENTER_FRAME, collide)
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
}
/*function collide(e:Event):void
{
if(char.hitTestObject(block))
{
char.visible = !char.visible;
}
}*/
function kDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
char1.x -= 5;
trace(char1.x);
break;
case Keyboard.RIGHT:
char1.x +=5;
trace(char1.x);
break;
}
}
}
}
You might want to consider writing a static Input class.
package input {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Input {
private static var keys:Array = new Array(255);
public static function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
private static function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
private static function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
public static function isKeyDown(k:int):Boolean {
return keys[k];
}
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
// The rest of the keys...
}
}
To use it first call setup() which adds the listeners for KEY_DOWN and KEY_UP events.
They you can easily query keys and do relevant actions accordingly.
Input.setup(stage);
/...
if(Input.isKeyDown(Input.A)) {
char1.x -= 5;
}

Projectiles not moving as they are supposed to

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
public function supportForce()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
}
GreenLight.x -= defaultSpeed;
}
}
}
So the projectile here is Projectile1 and it is declared as GreenLight in the keyCheck Function. So in the same function (the ENTER.FRAME function) GreenLight is supposed to move to its left. This does not happen and also an error saying TypeError: Error #1009: Cannot access a property or method of a null object reference.
at supportForce/keyCheck() appears. Thank you in advance.
In your keyCheck function, GreenLight will be null if _keyDownStatus[90] is false
so put the GreenLight as a class variable
private var lights:Array = [];
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
for each (var l:Projectile1 in lights)
{
l.x -= defaultSpeed;
}
lights.push(GreenLight);
}
else
{
//remove all Projectile1s in lights from stage and clear lights if you want
}
}
you create GreenLight in if condition scope and if keyCode isnot 90, object of Projectile1 isnot created and GreenLight.x -= defaultSpeed; there GreenLight is null

as3 addEventListner on a function in another class

I have a class calling a function in another class. I want to know
if we can add an Event Listener to know if that function has fired and is finished etc.
Here is the section of the code that is relevant.
myMC = new pictures();
addChild(myMC);
myMC.loadImages("Joyful",1)
// Add Event Listener here to let me know loadImages was called and worked.
As you can see the function is called loadImages() and it is located in a class called pictures.as
Can I add an event listener to this?
Here is a new issue. I am using a similar method and I used your example below and it did not work. Am I missing an import or something? I am only showing the functions that pertain and not my whole script.
Main.class
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.*;
import flash.filesystem.*;
import com.greensock.*;
import com.greensock.easing.*;
import flash.system.System;
// Ed's Scripts
import com.*;
import com.views.*;
public class iRosaryMain extends MovieClip
{
/// (Get Main Doc flow) this creates an instace of the main timeline
/// and then I send it
private static var _instance:iRosaryMain;
public static function get instance():iRosaryMain
{
return _instance;
}
/// Declaring Vars
var audio:audioPrayers;
/// Loading Images
//public var theImages:pictures = new pictures();
/// Movie Clips
public var myMary:beautifulMary;
public var myMC:MovieClip;
public var myPic:MovieClip;
public var myFlags:MovieClip;
public static var mt:MovieClip;
var vars:defaultVars = new defaultVars();
public function iRosaryMain()
{
// (Get Main Doc flow) Here I send the an instacne of iRosaryMain to defaultVars.as
_instance = this;
vars.getMainDoc(_instance);
// Sets timeline to mt :) I hope
mt = _instance;
audio = new audioPrayers();
trace("Jesus I trust in you!");// constructor code
audio.sayHailMary();
if (stage)
{
init();
}
}
public function moveLanguages()
{
/// File to languages folder
var checkLanguageFolder:File = File.applicationStorageDirectory.resolvePath("Languages");
///// CHECK LANGUAGES FOLDER - if not in App Storage move it
if (! checkLanguageFolder.exists)
{
var sourceDir:File = File.applicationDirectory.resolvePath("Languages");
var resultDir:File = File.applicationStorageDirectory.resolvePath("Languages");
sourceDir.copyTo(resultDir, true);
trace( "Moved Language!");
}
}
//// MAIN FUNCTIONS IN iRosaryMainClass
function init()
{
//loadFlags();
moveLanguages();
//addChild(theImages);
intro();
}
public function loadFlags()
{
myFlags.addEventListener("MyEvent", eventHandler);
myFlags = new viewLanguages();
addChild(myFlags);
myFlags.x = stage.stageWidth / 2 - myFlags.getBounds(this).width / 2;
function eventHandler()
{
trace("yes loaded");
TweenMax.fromTo(myMary,3, {alpha:1}, {alpha:0, ease:Quint.easeOut, onComplete: closePic} );
}
}
function intro()
{
myMary = new beautifulMary();
addChild(myMary);
loadFlags();
}
public function closePic()
{
removeChild(myMary);
}
public function languageLoaded(lang:String)
{
trace("YES... " + lang);
homeIn();
}
public function homeIn()
{
if (myFlags)
{
TweenMax.fromTo(myFlags,1, {x:myFlags.x}, {x:0-myFlags.width, ease:Quint.easeInOut, onComplete:unloadMyFlags} );
}
function unloadMyFlags()
{
trace("Called Ease out");
if (myFlags is MovieClip)
{
//trace("CURRENT DISPLAY " + currentDisplayScreen);
trace("CURRENT mc " + myFlags);
removeChild(myFlags);
System.gc();
myFlags = null;
trace("CURRENT mc " + myFlags);
}
}
if (! myMC)
{
myMC = new viewHome();
myMC.name = "Home";
myMC.x = stage.stageWidth / 2 - myMC.width / 2;
addChild(myMC);
}
theEaseIn(myMC);
//Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
}
public function loadLanguage(Language:String):Function
{
return function(e:MouseEvent):void ;
{
trace("Did it work? " +Language);
vars.loadButtonVars(Language);;
};
//TweenMax.fromTo(EnglishButton,1, {x:EnglishButton.x}, {x:0-EnglishButton.width, ease:Quint.easeOut} );
}
public function loadView(screen:String)
{
trace("Received " + screen);
if (screen=="Home")
{
myMC = new viewHome();
addChild(myMC);
theEaseIn(myMC);
//Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
}
if (screen=="PrayTheRosary")
{
myMC = new viewPrayTheRosary();
addChild(myMC);
theEaseIn(myMC);
//Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
}
if (screen=="Options")
{
myMC = new viewOptions();
addChild(myMC);
theEaseIn(myMC);
}
if (screen=="Downloads")
{
myMC = new viewDownloads();
addChild(myMC);
theEaseIn(myMC);
}
if (screen=="beautifulMary")
{
myMC = new beautifulMary();
addChild(myMC);
theEaseIn(myMC);
}
if (screen=="Flags")
{
myFlags = new viewLanguages();
addChild(myFlags);
theEaseIn(myFlags);
}
if (screen=="Joyful" || screen=="Sorrowful" || screen=="Glorious" || screen=="Luminous")
{
myPic = new pictures();
addChild(myPic);
myPic.addEventListener( "ImagesLoaded", doTheEaseIn);
myPic.loadImages(""+screen+"",1);
function doTheEaseIn()
{
theEaseIn(myPic);
}
}
}
public function theEaseIn(mc:MovieClip)
{
if (myMC)
{
TweenMax.fromTo(mc,1, {x:stage.stageWidth+mc.width}, {x:stage.stageWidth/2 - mc.width/2, ease:Quint.easeInOut} );
}
}
public function theEaseOut(mc:MovieClip,nextMC:String):Function
{
return function(e:MouseEvent):void ;
{
loadView(nextMC);
/// Tweens out view on screen
TweenMax.fromTo(mc,1, {x:mc.x}, {x:0-mc.width, ease:Quint.easeInOut, onComplete:unloadMC} );
function unloadMC();
{
trace("Called Ease out");
if(mc is MovieClip);
{
//trace("CURRENT DISPLAY " + currentDisplayScreen);
trace("CURRENT mc " + mc);
removeChild(mc);
System.gc();
myMC = null;
trace("CURRENT mc " + mc);
};
};
};/// end return
}
////////////
}/// End iRosaryMain
}// End Package
viewLanguages.as
package com.views
package com.views
{
import flash.display.MovieClip;
import flash.filesystem.File;
import com.roundFlag;
import flash.events.*;
import flash.display.*;
public class viewLanguages extends MovieClip
{
var Flag:MovieClip;
//public static const FLAGS_LOADED:String = "flagsLoaded";
public function viewLanguages()
{
getLang();
}
function numCheck(num:int):Boolean
{
return (num % 2 != 0);
}
/// Get for App Opening
public function getLang()
{
/// When Live
var folderLanguages:File = File.applicationStorageDirectory.resolvePath("Languages");
var availLang = folderLanguages.getDirectoryListing();
var Lang = new Array();
var LangPath = new Array();
var fl:int = 0;
for (var i:uint = 0; i < availLang.length; i++)
{
if (availLang[i].isDirectory)
{
//flag = new Loader();
//flag.load(new URLRequest(File.applicationStorageDirectory.url + "Languages/" + Lang[i] + "/roundFlag.png"));
Lang.push(availLang[i].name);
LangPath.push(availLang[i].nativePath);
Flag = new roundFlag(availLang[i].name);
Flag.name = availLang[i].name;
if (numCheck(i)==false)
{
Flag.y = fl;
Flag.x = 0;
}
else
{
Flag.y = fl;
Flag.x = Flag.width + 33;
fl = fl + Flag.height + 33;
}
Flag.btext.text = availLang[i].name;
addChild(Flag);
trace(availLang[i].nativePath);// gets the name
trace(availLang[i].name);
}
}
trace("Get Lang Called");
dispatchEvent(new Event("MyEvent"));
}
}
}
Yes, you can. Considering you are adding your clip to the display list I assume it extends either Movieclip or Sprite, both of which extend EventDispatcher. In your pictures class you can simply use dispatchEvent(new Event("MyEvent"); to dispatch an event. Then you could add myMC.addEventListener("MyEvent", eventHandler); to react to it.
You should also not use strings for events like I wrote above. It would be better if you declared a public static const IMAGES_LOADED:String = "imagesLoaded"; in your pictures class. Then you should use this constant by dispatching and by listening to it.
EDIT: Here how it would look with the constant:
//your class
package {
import flash.events.Event;
import flash.display.Sprite;
public class Pictures extends Sprite {
public static const IMAGES_LOADED:String = "imagesLoaded";
public function Pictures() {
}
public function onAllImagesLoaded():void {
dispatchEvent(new Event(IMAGES_LOADED));
}
//rest of your methods
}
}
//you would call it like this:
var myMc:Pictures = new Pictures();
myMc.addEventListener(Pictures.IMAGES_LOADED, onImagesLoaded);
myMc.loadImages();
function onImagesLoaded(e:Event):void {
//...etc
}
You have to create custom event class for this to work. Like:
package com.some
{
import flash.events.Event;
public class SomeEvent extends Event
{
// Loading events
public static const MAINDATA_LOADING:String = "onMainDataLoading";
// Other event
public static const SOME_LOADING:String = "onSomeLoading";
public var params:Object;
public function SomeEvent($type:String, $params:Object, $bubbles:Boolean = false, $cancelable:Boolean = false)
{
super($type, $bubbles, $cancelable);
this.params = $params;
}
public override function clone():Event
{
return new SomeEvent(type, this.params, bubbles, cancelable);
}
}
}
Add event dispatch inside wanted class (Pictures) and pass wanted params to event (this or any else)
dispatchEvent(new SomeEvent(SomeEvent.IMAGES_LOADED, this));
Handle event listening:
var myMc:Pictures = new Pictures();
myMc.addEventListener(SomeEvent.IMAGES_LOADED, onImagesLoaded);
myMc.loadImages();
function onImagesLoaded(e:SomeEvent):void {
// handle event.
var picts:Pictures = (e.params as Pictures);
}

AS3 TypeError: Error #2007: Parameter hitTestObject must be non-null

TypeError: Error #2007: Parameter hitTestObject must be non-null.
I am working on a game right now and trying to get it so the player will collide with the ground (he already collides with table)
It says the problem is happening in playerOneCollisions
table and ground are just classes with the basic class code linked to movieclips,
they are extended movieclips (if that matters), and they have imported stage and movieclip (again, if that matters)
(the parts I think are important)
Player.as
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
The function playerOneCollisions is called in Main.as
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
The rest of the code incase I was wrong about the important parts
Player.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import KeyObject;
import Table;
public class Player extends MovieClip
{
public var stageRef:Stage;
public var key:KeyObject;
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var grounded:Boolean = false;
public var animation:String = "idle";
public var runSpeed:Number = 5;
public var animationState:String = "idle";
public function Player (stageRef:Stage, X:int, Y:int):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
key = new KeyObject(stageRef);
}
public function playerOnePress()
{
if(key.isDown(37) || key.isDown(65)){ // if left arrow or A is pressed
leftPressed = true;
//trace("left pressed");
} else {
leftPressed = false;
}
if(key.isDown(38) || key.isDown(87)){ // if up arrow or W is pressed
upPressed = true;
//trace("up pressed");
} else {
upPressed = false;
}
if(key.isDown(39) || key.isDown(68)){ //if right arrow or D is pressed
rightPressed = true;
//trace("right pressed");
} else {
rightPressed = false;
}
if(key.isDown(40) || key.isDown(83)){ //if down arrow or S is pressed
downPressed = true;
//trace("down pressed");
} else {
downPressed = false;
}
}
public function playerOneKeyResults()
{
if(leftPressed)
{
// trace("left");
this.x -= runSpeed;
}else if(rightPressed)
{
// trace("right");
this.x += runSpeed;
}
}
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
public function playerAnimaitons()
{
if(animation == "kong")
{
this.gotoAndStop(2);
}
if(animation == "idle")
{
this.gotoAndStop(1);
}
}
}
}
Main.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends MovieClip
{
public var gameTimer:Timer;
public var player:Player;
public var table:Table;
public var ground:Ground;
public function Main():void
{
gameTimer = new Timer (30);
gameTimer.addEventListener(TimerEvent.TIMER, GameClock);
gameTimer.start();
player = new Player(stage, 80, 420);
stage.addChild(player);
}
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
}
}
One thing I notice is that you have table and ground as class properties of Player, and they are also parameters for the method you are having trouble with.
The table and/or ground properties of your Player class are likely null as I don't see code that sets them.
It's not good practice to have method parameters named the same as your class properties.
Try removing the properties in your Player class. (I don't see them being set or used)
If you still get the error, trace out their values at the beginning of the playerOneCollisions method and ensure they are not null.
If they are null, the problem exists outside this class and you are passing in a null value.
I should also note that I don't see anywhere in the Main class where you set the table or ground properties, so they are null unless you have not shown us all the code. If they are null, then that will certainly cause the issue you are experiencing.

Can I assign a getter function to a variable in AS3?

I can do
function yea ():int {
...
}
var cool:Function=yea;
I don't know how to do something like
function get wat ():int {
...
}
var mmmh:Function=wat; // this (tries to) assign the wat returned value
Well, the only way I know is capturing getter/setter references from inside of them with arguments.callee. But that's a rather weird thing to do. :)
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.getTimer;
public class GetterTest extends Sprite
{
private var getterRef:Function;
private var setterRef:Function;
private var testValue:int = 0;
public function GetterTest()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
private function onMouseDown(event:Event):void
{
if (getterRef != null) {
setterRef(getTimer());
trace("references: test = ", getterRef());
} else {
test = getTimer();
trace("direct: test = ", test);
}
}
private function get test():int
{
trace("TEST! getter");
getterRef = arguments.callee;
return testValue;
}
private function set test(value:int):void
{
trace("TEST! setter");
setterRef = arguments.callee;
testValue = value;
}
}
}
While the above works, I think the correct answer to your question is "don't". :)
you could save the name of the getter as a string var fn:String = "wat" and then use it later to retrieve the return value of the method with myObjectFromClass[fn].
I am not sure what is that you are trying to accomplish.
If you just want to keep a reference to the getter, you could just wrap it in a closure function.
var mmh:Function = function():int { return wat; };