I am trying to place API variables on ONE class file or mxml file and call these variables in other random class or mxml files... any suggestions?
I just create a file in any package and called it api.as. Inside there, there's just static member variables.
public static var foo:String = "Bar";
public static var bar:Number = "100";
...
Then you just include import <package_path>.api.as in any files that want to use it.
EDIT: Already accepted, but answering the question:
package com.foo {
public class Api {
public static const FOOBAR:String = "foobar";
}
}
Related
I'm making a mod, and I am getting an error(no duh) and I have tried searching it up but I want an answer specific to my problem because I am not very good at this. I am getting this error in my block class.
Implicit super constructor Block() is undefined for default constructor. Must define an explicit constructor
and I don't know how to fix it. Please Help its for a project.
block class:
package GDMCrocknrollkid.fandomcraft;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
public class BlockCbBlock extends Block {
protected BlockCbBlock(Material material) {
super(material);
}
}
mod class:
package GDMCrocknrollkid.fandomcraft;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
#Mod(modid = "fc", name = "Fandomcraft", version = "1.0")
public class fandomcraft {
public static Item itemCbIngot;
public static Block blockCbBlock;
#EventHandler
public void preInit(FMLPreInitializationEvent event){
//Item/Block initialization and registering
//Config Handling
itemCbIngot = new ItemCbIngot().setUnlocalizedName("ItemCbIngot").setTextureName("fc:itemCbIngot"); //item.itemCbIngot.name
blockCbBlock = new BlockCbBlock(Material.iron);
GameRegistry.registerItem(itemCbIngot, itemCbIngot.getUnlocalizedName().substring(5));
}
#EventHandler
public void init(FMLInitializationEvent event){
//Proxy, TileEntity, entity, GUI and Packet Registering
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
This error pertains to all of java, not just minecraft forge. Check this for some more reference. There are a couple possible reasons for this error. It is most likely 1, but 2 and 3 can be a contributing factor to the error.
Your BlockCbBlock Class declares a constructor that is not the default, no-argument constructor that the compiler would otherwise provide (that is, if the Block class doesn't have a constructor) and, if in fact the Block class is using the default constructor, then you can't call super() on the arguements because the Block class uses a constructor with no arguments. Because of this, if you wanted to modify the Block constructor, it would be safier and easier to create a custom construcotr inside of the BlockCbBlock class itself.
You are trying to inherit the constructor of Block, but you have declared it as protected, when the constructor in your class should be public to match the inherited .
If you're using Eclipse, it can give this error when you have your project setup incorrectly (system configuration mismatch)
Probably not directly realted to this specific error, but a possible cause of other errors in the near future; you are using the annotation #EventHandler, but you have not actually declared the forge event handler.
You don't actually register the block for some reason. Even if you're using the block as a recipe item, you still need to register it
To fix potential problems 1, 2, and 4, try this (obtained from here):
package GDMCrocknrollkid.fandomcraft;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
private final String name = "BlockCbBlock";
public class BlockCbBlock extends Block {
public BlockCbBlock() {
super(Material.iron);
GameRegistry.registerBlock(this, name);
setUnlocalizedName(Reference.MODID + "_" + name);
setCreativeTab(CreativeTabs.tabBlock);
}
public String getName() {
return name;
}
}
This way, you'll declare its UnlocalizedName, Material, and CreativeTab ahead of time. This method might be unnecessary, but its a good precaution to help prevent the error. Now, all you have to do is declare it like this:
//You need to make your own EventHandler class. Search online for that.
FCEventHandler handler = new FCEventHandler();
#EventHandler
public void preInit(FMLPreInitializationEvent event){
//Config Handling
//event handler registry
FMLCommonHandler.instance().bus().register(handler);
MinecraftForge.EVENT_BUS.register(handler);
//the same thing can be similarly done with this if you wish
itemCbIngot = new ItemCbIngot().setUnlocalizedName("ItemCbIngot").setTextureName("fc:itemCbIngot");
blockCbBlock = new BlockCbBlock();
GameRegistry.registerItem(itemCbIngot, itemCbIngot.getUnlocalizedName().substring(5));
}
I had study 「Beware of singleton in Flex modules」
in http://www.devahead.com/blog/2010/03/beware-of-singleton-in-flex-modules/
and a lot of information tell me that different module with different content,but in my case it doesn't work!
why the different module use the same static object?
I'm trying to use module wide singleton,but it work like application wide singleton.
Can someone help me how to make module wide singleton.
the short code is like:
<s:Application>
<s:ModuleLoader id="A" creationComplete="loadAModule()"/>
<s:ModuleLoader id="B" creationComplete="loadBModule()"/>
</s:Application>
//-----------AModule
<s:Module>
var aITx:ITx=Tx.newInstant();//Tx extend ITX
tracc(aITx.instantId);
...
</s:Module>
//-----------BModule
<s:Module>
var aITx:ITx=Tx.getInstance();//Tx extend ITX
tracc(aITx.instanceID);
...
</s:Module>
//-----singleton class
public class Tx extends EventDispatcher implements ITx
{
public function Tx()
{
// Add listeners
addEventListeners();
}
private static var instance:Tx;
public static function getInstance():Tx
{
if (!instance)
{
instance = new Tx();
// Generate a random instance ID
instance._instanceID = Math.round(Math.random() * 100);
trace("create new itx id="+instance.instanceID);
}else{
trace("use old itx id="+instance.instanceID);
}
return instance;
}
protected var _instanceID: Number = NaN;
public function get instanceID(): Number
{
return _instanceID;
}
}
I think your problem is related to the context the modules are loaded in. In your case I guess all of your modules are loaded into the same context. In one context there is only one version of one class. Therefore there is only one instance of your Singleton. If you want to have separate classes, you have to load each module into its own context. Have a look at this link which explains the context stuff pretty good: http://livedocs.adobe.com/flex/3/html/help.html?content=05_Display_Programming_33.html
So here's what I mean.
Let's say I have a class ErrorMessages which holds all my error messages as static constants. So I could access them like ErrorMessages.PASSWORD_INVALID or ErrorMessage.PASSWORD_TOO_SHORT. I want to know if it is possible to have separate classes that hold subset of these constants and access them like ErrorMessages.PASSWORD.INVALID or ErrorMessages.PASSWORD.TOO_SHORT, etc.
This way I can more structured static structure and makes it much easier to use autocomplete.
I tried few different ways and couldn't figure out if this was possible..
Declare them as const Objects in the static class - you won't get them in auto complete though.
public class ErrorMessages
{
public static const PASSWORD:Object = {
INVALID:"invalid password",
TOO_SHORT:"minimum 6 chars required",
TOO_LONG:"100 chars: r u sure?"
};
public static const FILE:Object = {
NOT_FOUND:"No such file",
READ_ONLY:"it is readonly",
SOMETHING_ELSE:"something else"
};
}
trace(ErrorMessages.PASSWORD.INVALID);
If auto complete is important, create a dedicated com.domain.errors package and declare different classes for different categories of errors (like PASSWORD, FILE etc) within that package. Now declare public static constants inside those classes as appropriate.
or if you want to keep a single class, you can define classes, inside your Error class. You might would like to have those text coming from properties file. So, you can make use of resourceManager instance and get the text from specific resource bundle.
--
http://riageeks.com
Here's what I end up doing
package com.domain.data.type {
public class ErrorMessages {
public static function get PASSWORD:PasswordErrorMessages { return new PasswordErrorMessages(); }
}
}
class PasswordErrorMessages {
public function get INVALID():String { return "invalid password"; }
}
This way I can get the behavior I wanted: ErrorMessages.PASSWORD.INVALID with autocomplete. It's not as clean as I'd like it to be.. but I guess this will do.
I have an ugly problem. I have two string variables (className and staticMethod) store the name of a class and it's static method I have to call:
package {
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
public class ClassPlay extends Sprite {
public function ClassPlay() {
new Foo();
var className:String = 'Foo';
var staticMethod:String = 'bar';
var classClass:Class = getDefinitionByName(className) as Class;
try {
classClass[staticMethod]();
} catch (e:Error) {}
}
}
}
This is the subject class:
package {
public class Foo {
public static function bar():void {trace('Foo.bar() was called.');}
}
}
It works just perfectly. The problem when you comment out this (9th) line:
// new Foo();
Without this line it exits with an exception:
ReferenceError: Error #1065: Variable Foo is not defined.
How could I do this without that instantiation? If that is impossible, is there a way to instantiate the class from the string variable? Or if it's still a bad practice, how would you do that? (I have to work with those two unknown string variable.)
Thanks in advance.
The reason is that the compiler will strip out unnecessary classes - if you don't have an explicit reference to the class Foo somewhere, it won't be present in your final application.
You could the reference elsewhere and still force it to be loaded - for example, a static array of references to the classes.
It should work if you just throw in a trace(classClass) - that should give you the reference you need, if I remember this stuff correctly.
I have two Cairngorm MVC Flex applications (a full version and lite version of the same app) that share many Classes. I have put these Classes into a Flex Library Project that compiles as an SWC. Both applications use some static String constants. Right now, I am storing these in the ModelLocator:
package model
{
[Bindable]
public class ModelLocator
{
public static var __instance:ModelLocator = null;
public static const SUCCESS:String = "success";
public static const FAILURE:String = "failure";
public static const RUNNING:String = "running";
...
}
}
This just doesn't seem like the best place to store these constants, especially now that they are used by both applications, and I have setup each application to have its own ModelLocator Class. Plus this is not the purpose of the ModelLocator Class.
What would be a good way to store these constants in my shared Library?
Should I just create a Class like this?:
package
{
[Bindable]
public class Constants
{
public static const SUCCESS:String = "success";
public static const FAILURE:String = "failure";
public static const RUNNING:String = "running";
}
}
and then reference it like this:
if (value == Constant.SUCCESS)
...
I'd say organize the constants by logical meaning, instead of a single Constants class.
Say you have the 3 you show as some kind of task state, and you have some more that are used as error codes for file access (just making stuff up here):
public class TaskStates {
public static const SUCCESS:String = "success";
public static const FAILURE:String = "failure";
public static const RUNNING:String = "running";
}
public class FileErrors {
public static const FILE_NOT_FOUND:String = "filenotfound";
public static const INVALID_FORMAT:String = "invalidformat";
public static const READ_ONLY:String = "readonly";
}
I find this makes it easier to document what the expected values are for something. Instead of saying "Returns either SUCCESS, FAILURE, RUNNING, ...", you can just say "Returns one of the TaskState.* values).
You could put all these in a single package for constants, or you could have the constant classes live in the same package as the classes that use them.