Any way to Call A Function As A String? - actionscript-3

I've just started a project, and am wondering if you can call a function (in an event listener) through a string.
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
var threesec:Timer=new Timer(3000, 1);
var whaton:String="tsecc"
threesec.start();
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
function tsecc(tsecc:TimerEvent):void{
trace("Hello")
threesec.reset();
threesec.start();
}
This does not work because of this line:
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
and this error code:
1067: Implicit coercion of a value of type String to an unrelated type Function.
I know what I am doing is horribly wrong, but is there a correct way to call a function in string format?
Do I have to add a property to the variable, do I have to create another type of variable?

threesec.addEventListener(TimerEvent.TIMER_COMPLETE, this[whaton]);
I used "bracket syntax" to do this. You can learn more about it by searching on the Internet.

Related

Can't write to a csv file from a class in kotlin

Im trying to write a class to csv. And I need or at least prefer a class that I could call that could both retrieve and eventually save the class objects as csv.
I have tried every imaginable way I can find to write files. Everytime it works flawlessly in the main activity .kt, but soon as I move any of it to a dedicated class I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
or I get told thats is a read only file system.
Heres a copy of a class that ive tried and gotten the error.
package com.example.jobndays
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.PrintWriter
class DryTest: AppCompatActivity() {
fun main() {
var fileInputStream: FileOutputStream? = null
fileInputStream = openFileOutput("bobbin.csv", MODE_PRIVATE)
val bob = "fella"
fileInputStream.write(bob.toByteArray())
}
}
I believe I've found an answer. Apparently in order to write files you need 'context', not entirely sure why, but just ask for it and stick infront of stuff that says they're unresolved.

actionscript 3 - Error #2136

So im trying to understand how I can call a function from one class from another class. Im getting a few errors and am wondering if someone can explain what im doing wrong here.
Main file:
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.*;
import code.functions.*;
public class Main extends MovieClip {
public var _playerHP:Number;
public var _enemyYellow:EnemyYellow;
public function Main() {
_enemyYellow = new EnemyYellow;
_playerHP = 10;
_playerHPdisplay.text = _playerHP.toString();
trace("loaded")
}
public function lowerHP ():void
{
_playerHP -= 1;
_playerHPdisplay.text = _playerHP.toString();
trace(_playerHP)
}
}
}
Second File:
package code.functions {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class EnemyYellow extends MovieClip {
public var _main:Main;
public function EnemyYellow() {
_main = new Main;
_main.lowerHP();
trace ("test")
}
}
}
It will then load with a blackscreen and the following error:
Error: Error #2136: The SWF file file:///test/Main.swf contains invalid data.
at code.functions::EnemyYellow()[test\code\functions\EnemyYellow.as:15]
at code::Main()[test\code\Main.as:16]
Error opening URL 'file:///test/Main.swf'
However, If I remove _enemyYellow = new EnemyYellow; from the Main file it loads but the second file is not loaded.
If I remove _main = new Main; from the Second file, the game again loads but it does not call the lower HP function, and I get the following error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code.functions::EnemyYellow()[test\code\functions\EnemyYellow.as:16]
at code::Main()[test\code\Main.as:16]
If anyone could help me it would be appreciated. Im just trying to get my head around how to call a function from another file..
_playerHPdisplay.text is also a text box on the stage when the game loads.
If you do not assign a value to _main, it is null. That's why you receive the #1009 if you do not assign new Main() to it.
However, you do not want to create a new Main object either.
The main class represents the application and generally speaking you do no explicitly instantiate it in your project.
To make your code work, you have to pass a reference of Main to the enemy class.
A better approach to this is to let the enemy class dispatch events, so that the Main class can be notified "some damage was dealt". This however will not work from within the constructor of enemy.
Think about whether your package names make sense. Pretty much all packages contain code, which makes "code" a not very informative name. The package "functions" contains the class EnemyYellow, which doesn't seem to be a good fit.

1137: Incorrect number of arguments. Expected no more than 0. statement copied from tutorial but not working

I was following an adobe tutorial in which we make a text field and the text i update in it is from function sayHello()
import flash.display.MovieClip
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.text.TextField;
var myGreeter:Greeter = new Greeter();
mainText.text = myGreeter.sayHello("Bob");
This is written in first frame^^^^^
SayHello function is in the other actionscript file in same folder with the following code
package
{
import flash.display.MovieClip;
public class Greeter
{
public function sayHello():String
{
var greeting:String;
greeting = "Hello World!";
return greeting;
}
}
}
Maybe some would ask that did you put a TextField on the stage and give it an instance name and the answer is yes i did.
The tutorial i followed i don't know why after telling code told us correct errors if there are in it so there is a possibility that they wanted to train us maybe.
i am a little confuse with greeter class myself as why we write
sayHello("Bob")
Why not
sayHello()
i say this because the variable only has string hellow world what it has to with that man Bob
It would be kind of you if you can also explain me that,
I am asking this too becuase i also need to have complete understanding of code.
I'm not sure, but you may have conflated two steps in the tutorial. You're right that with your definition of sayHello, you should call
sayHello();
To have the function take an argument, you need to define the function to take an argument:
public function sayHello(user:String):String {
return "Hello, " + user + "!";
}
You would then call:
sayHello('Hamza');
and it would return
"Hello, Hamza!"
In short words: "The tutorial is wrong or it is incomplete". You call sayHello with one param but sayHello are declared without params. And the compiler give you right error for this call.

How to defiine class variable not importing package with it?

for example is it possible to do somehing like public var socket:flash.net.Socket = new flash.net.Socket();?
No you will still get a class undefined compiler error if you don't import flash.net.Socket

Reference to Parent MXML within an AS3 Class?

I have an AS3 class on my Flex project:
package system
{
import mx.managers.PopUpManager;
import ui.Eula;
public class Dialogs
{
public function Dialogs(){}
public static function showEula():void {
var eulaWindow:Eula = new Eula;
PopUpManager.addPopUp(eulaWindow,MyMainMXML,true);
}
}
}
MyMainMXML is my base MXML file. It won't let me reference to it via my class. How do I do that? The compiler error goes as follows:
1067: Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject.
The main MXML file is a spark WindowedApplication so I assumed it's part of the DisplayObjects.
EDIT:
I tried using FlexGlobals like the one below but it gives off an error that says 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
package system
{
import mx.core.FlexGlobals;
import mx.managers.PopUpManager;
import ui.Eula;
public class Dialogs
{
public function Dialogs(){}
public static function showEula():void {
var eulaWindow:Eula = new Eula;
PopUpManager.addPopUp(eulaWindow,FlexGlobals.topLevelApplication,true);
}
}
}
Using FlexGlobals.topLevelApplication returns you an object of type Object (yeah I know, that sounds redoundant :P). However addPopUp 2nd parameter if a DisplayObject. Hence, this should do the trick :
PopUpManager.addPopUp(eulaWindow,FlexGlobals.topLevelApplication as DisplayObject,true);
I'm not 100% sure about why FlexGlobals.topLevelApplication does not return a DisplayObject, might be a low-level issue.
You can got main application refference from
FlexGlobals.topLevelApplication
mx.core.FlexGlobals.topLevelApplication: The top-level application object, regardless of where in the document tree your object executes. This object is of type spark.components.Application or mx.core.Application.