Basic class understanding - trying to understand AS3
I'm relatively new to AS3 and AS in general so forgive me...
Is my understanding that there are no Abstract classes and
you can't override or overload constructors of derived classes
correct? Abstract classes aren't important for me but if it's
possible I'd like to know how. But is there a way to overload
default constructors and I'm just doing something wrong? Can we
only override methods or can we overload them also. Work arounds
would also be appreciated.
Here's what I'm doing currently to make things work.
-For a simple game I'm making, I'm not using FlexBuilder and
don't want to for this project for specific reasons, so I'm using
Flash and instead of using sprites and bitmaps I want to draw
shapes in Flash and covert them into MovieClip symbols which I
"link" for export as AS to use in my code. I want to make it a
little modular so I can just change certain aspects of the code for
different approaches.
If I create a class Actor which extends MovieClip the default
constructor has to be Actor() with no arguments. But I'd like my
class to be able to have arguments like initial x and y
coordinates. I know you can just create methods to initialize each
part but I'd like to try and have a default constructor to do so.
In Flash I notice that if you derive a written class or built
in class it requires the standard default constructor, "which I may
be wrong about and I hope I am and someone could show me what I'm
doing wrong", I found if you derive a class based on a symbol you
linked for export as AS I can create a new constructor with
whatever argument(s) I want (so this is why I'm making this post,
if I can do this couldn't there be a way to override
constructors?).
Here's how I'm doing it currently...
1) So I create a base class called Actor which extends
MovieClip save it as Actor.as and default constructor of Actor()
add whatever properties I want.
2) Then I create an object in my library, let's call it
mcShip, then I link that symbol for export as ActionScript and have
it's base class as Actor and name the class ShipMC.
3) I then create a new .as file and call this one
ShipActor.as, this class ShipActor extends ShipMC and then I create
a default constructor ShipActor(gs:GameScreen, xA:Number,
yA:Number) and this works fine
(also I create a method in Actor class called ActorSuper and
call it from ShipActor with passed in arguments, since I don't know
of a way to overload the parent class' constructor)
If I were to just have my mcShip with a base class of
flash.display.MovieClip and I try to create ShipActor with the
constructor above it says missing default constructor. The above 3
steps is the only way I can get it to work for now. Is there a way
to overload the constructor? If not are there any other work
arounds?
I'm only looking at this for object oriented approach more
than anything. Sorry for the lengthy post but I didn't want any
confusion, or any more confusion -.-
Thanks for taking the time to read this post
-John
Your correct in that you cannot define abstract classes in
ECMA based actionscript nor does Actionscript allow you to overload
at all. There are work arounds for both though such as just have
your overloaded methods use a single Object as its argument and
that Object contain the different parameters.
I find developing in the Flash IDE difficult at best so if
you'd like another option besides flex builder you can use eclipse
webtool kit with FDT. Its only got 30 days free but its worth it to
buy it ( trust me ). A tip though is to install the latest flex sdk
and change the source library to it so you can use Ant to build
your swf's.
For an object called Actor I would just extends the Sprite
package instead of MovieClip unless you're going to manipulate its
timeline ( why would you though ). If you need an tweening package
check out GreenSock's TweenLite. Its better then flash's default
one.
Similar Messages
-
Please help with simple Classes understanding
Working further to understand Class formation, and basics.
At the Java Threads Tutorial site:
http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
Goal:
1)To take the following code, and make it into 2 seperate files.
Reminder.java
RemindTask.java
2)Error Free
Here is the original, functioning code:
import java.util.Timer;
import java.util.TimerTask;
* Simple demo that uses java.util.Timer to schedule a task
* to execute once 5 seconds have passed.
* http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
public class Reminder {
Timer timer;
public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer thread
public static void main(String args[]) {
new Reminder(5);
System.out.println("Task scheduled.");
}Here is what I tried to 2 so far, seperate into 2 seperate files:
Reminder.java
package threadspack; //added this
import java.util.Timer;
import java.util.TimerTask;
* Simple demo that uses java.util.Timer to schedule a task
* to execute once 5 seconds have passed.
* http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
public class Reminder {
Timer timer;
public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
public static void main(String args[]) {
new Reminder(5);
System.out.println("Task scheduled.");
}and into
RemindTask.java
package threadspack; //added this
import java.util.Timer;
import java.util.TimerTask;
import threadspack.Reminder; //added this
* http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
public class RemindTask extends TimerTask
Timer timer; /**here, I added this, because got a
"cannot resolve symbol" error if try to compile w/out it
but I thought using packages would have negated the need to do this....?*/
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer thread
}After executing Reminder, the program does perform, even does the timing, however, a NullPointerException error is thrown in the RemindTask class because of this line:
timer.cancel(); //Terminate the timer thread
I am not sure of:
If I have packages/import statements setup correctly
If I have the "Timer" variable setup incorrectly/wrong spot.
...how to fix the problem(s)
Thank you!Hi there!
I understand that somehow the original "Timer" must
be referenced.
This is a major point of confusion for me....I
thought that when importing
Classes from the same package/other packages, you
would have directly
access to the variables within the imported Classes.I think you have one of the basic points of confussion. You are mixing up the concept of a "Class" with the concept of an "Object".
Now, first of all, you do not need packages at all for what you are trying to do, so my advice is you completely forget about packages for the moment, they will only mess you up more. Simply place both .class files (compiled .java files) in the same directory. Your program is executing fine, so that indicates that the directory in which you have your main class file is being included in your classpath, so the JVM will find any class file you place there.
As for Classes/Objects, think of the Class as the map in which the structure of a building is designed, and think of the Object as the building itself. Using the same technical map the architect defines, you could build as many buildings as you wanted. They could each have different colors, different types of doors, different window decorations, etc... but they would all have the same basic structure: the one defined in the technical map. So, the technical map is the Class, and each of the buildings is an object. In Java terminology, you would say that each of the buildings is an "Instance" of the Class.
Lets take a simpler example with a class representing icecreams. Imagine you code the following class:
public class Icecream{
String flavor;
boolean hasChocoChips;
}Ok, with this code, what you're doing is defining the "structure" of an icecream. You can see that we have two variables in our class: a String variable with the description of the icecream's flavor, and a boolean variable indicating whether or not the icecream has chocolate chips. However, with that code you are not actually CREATING those variables (that is, allocating memory space for that data). All you are doing is saying that EACH icecream which is created will have those two variables. As I mentioned before, in Java terminology, creating an icecream would be instantiating an Icrecream object from the Icecream class.
Ok, so lets make icrecream!!!
Ummm... Why would we want to make several icecreams? Well, lets assume we have an icecream store:
public class IcecreamStore{
}Now, we want to sell icecreams, so lets put icecreams in our icecream store:
public class IcecreamStore{
Icecream strawberryIcecream = new Icecream(); //This is an object, it's an instance of Class Icecream
Icecream lemonIcecream = new Icecream(); //This is another object, it's an instance of Class Icecream
}By creating the two Icecream objects you have actually created (allocated memory space) the String and boolean variable for EACH of those icecreams. So you have actually created two String variables and two boolean variables.
And how do we reference variables, objects, etc...?
Well, we're selling icecreams, so lets create an icecream salesman:
public class IcecreamSalesMan{
}Our icecream salesman wants to sell icecreams, so lets give him a store. Lets say that each icecream store can only hold 3 icecreams. We could then define the IcecreamStore class as follows:
public class IcecreamStore{
Icecream icecream1;
Icecream icecream2;
Icecream icecream3;
}Now lets modify our IcecreamSalesMan class to give the guy an icecream store:
public class IcecreamSalesMan{
IcecreamStore store = new IcecreamStore();
}Ok, so now we have within our IcecreamSalesMan class a variable, called "store" which is itself an object (an instance) of the calss IcecreamStore.
Now, as defined above, our IcecreamStore class will have three Icecream objects. Indirectly, our icecream salesman has now three icecreams, since he has an IcecreamStore object which in turn holds three Icecream objects.
On the other hand, our good old salesman wants the three icecreams in his store to be chocolate, strawberry, and orange flavored. And he wants the two first icecreams to have chocolate chips, but not the third one. Well, here's the whole thing in java language:
public class Icecream{ //define the Icecream class
String flavor;
boolean hasChocoChips;
public class IcecreamStore{ //define the IcecreamStore class
//Each icecream store will have three icecreams
Icecream icecream1 = new Icecream(); //Create an Icecream object
Icecream icecream2 = new Icecream(); //Create another Icecream object
Icecream icecream3 = new Icecream(); //Create another Icecream object
public class IcecreamSalesMan{ //this is our main (executable) class
IcecreamStore store; //Our class has a variable which is an IcecreamStore object
public void main(String args[]){
store = new IcecreamStore(); //Create the store object (which itself will have 3 Icecream objects)
/*Put the flavors and chocolate chips:*/
store.icecream1.flavor = "Chocolate"; //Variable "flavor" of variable "icecream1" of variable "store"
store.icecream2.flavor = "Strawberry"; //Variable "flavor" of variable "icecream2" of variable "store"
store.icecream3.flavor = "Orange";
store.icecream1.hasChocoChips = true;
store.icecream2.hasChocoChips = true;
store.icecream3.hasChocoChips = false;
}And, retaking your original question, each of these three classes (Icecream, IcecreamStore, and IcecreamSalesMan) could be in a different .java file, and the program would work just fine. No need for packages!
I'm sorry if you already knew all this and I just gave you a stupid lecture, but from your post I got the impression that you didn't have these concepts very clear. Otherwise, if you got the point, I'll let your extrapolate it to your own code. Should be a pice of cake! -
Classname.class - understanding a difference
Hallo,
i was looking trough some NetBeans samples, and I've seen
launch(MarsRoverViewerApp.class, args);I've tried to replce it with this:
launch(Class.forName("MarsRoverViewerApp").getClass(), args);Why isn't it working. maybe I'm tring something foolish, as I haven't seen any explication for classname.class thing. i don't understand properly what it does and how I should use it > maybe you have a good reference for this?ryanP wrote:
Hallo,
i was looking trough some NetBeans samples, and I've seen
launch(MarsRoverViewerApp.class, args);I've tried to replce it with this:
launch(Class.forName("MarsRoverViewerApp").getClass(), args);Why isn't it working. maybe I'm tring something foolish, as I haven't seen any explication for classname.class thing. i don't understand properly what it does and how I should use it > maybe you have a good reference for this?Since Class.forName returns an instance of Class, calling getClass on it will always return java.lang.Class.Don't call getClass on it, you already have a reference to MarsRoverViewerApp.class. -
Basic mis-understanding about Airport?
I cannot find a question like this, though I am sure they are there.
I have a base-station which works as advertised. I wish to extend my range, by using the Airport Express. I programmed the express to join an existing network. In another room, I have hooked up the Express to an outlet hardwired to the network, thinking that the express would join the network in a "wired" manner then rebroadcast it "wirelessly". I think this assumption, from what I have read is incorrect?
So, is there a way of doing what I am trying to do? ie...extend the wireless network by using the express in a "wired" fashion?
Thanks in advance
PB_G4 Mac OS X (10.4.8)Absolutely. It's called a roaming network. In short, what you need to do is set up the Express to create a wireless network with the same name as your existing network and then go into the Express's configuration in Airport Admin Utility and disable "Distribute IP addresses" in the Network tab. If you want to use Airport Setup Assistant to do the initial configuration, you'll need to perform a hard reset on the Express (holding down the reset button until the LED starts blinking rapidly).
-
Pls recommend good books on Spring framework for basic & easy understanding
Thanks in adv.
Hi Dina,
...the best Source for Infromation is still here ;-)
I´m working since 3 Years with the OWB, in different Versions and i think
here´re a lot of Proffessionals in these Forum.
So, if you´ve an explizit Question, post and post and post :-)
Documetations you can also found here :
http://www.oracle.com/technology/documentation/warehouse.html
and here:
http://www.tu-ilmenau.de/fakia/DWT-UEbungen.5382.0.html (german)
and a blog:
http://blogs.oracle.com/warehousebuilder/
Regards and lots of Luck in the new Job
Lone -
"Export classes in frame x" in AS3: how does it work?
My steps:
1. I have some beefy classes
2. I want my SWF to show a preload anim while it loads these
classes
3. Using the "Export classes in frame: " option, I move my
classes to frame 10
4. I put a stop(); on the first frame and test w/ the
bandwidth profiler visible.
Desired + expected behavior:
1. My preloader spins, and I have minimal K on the first
frame and then the main hit on the chosen export frame.
Observed behavior:
1. all of my classes are available on the first frame (ala,
all of my logic can be performed on the first frame, even though
the framehead is sitting firmly on frame 1)
2. there is still a spike in K on the output frame
Notes:
- am I just missing the point here? Is this how it always
worked?
- why is the Document Class available in the first frame? Is
this mandatory w/ the new structure?
Thanks for your time and help-
cgsThanks, moccamaximum, I believe this did open up a connection and now I have no errors.
However, my problem still persists.
Here is what it says in the output of my AS3 flash site:
_root
SWFBridge (AS3) connected: host
SWFBridge (AS2) connected as client
imgNum: 10
thumb: images/image1.jpg
image: images/image1.jpg
thumb: thumbs/image2.jpg
image: images/image2.jpg
thumb: thumbs/image3.jpg
image: images/image3.jpg
thumb: thumbs/image4.jpg
image: images/image4.jpg
thumb: thumbs/image5.jpg
image: images/image5.jpg
thumb: thumbs/image6.jpg
image: images/image6.jpg
thumb: thumbs/image7.jpg
image: images/image7.jpg
thumb: thumbs/image8.jpg
image: images/image8.jpg
thumb: thumbs/image9.jpg
image: images/image9.jpg
thumb: thumbs/image10.jpg
image: images/image10.jpg
4
_root
4
163
_root
163
67
_root
67
43
_root
43
56
_root
56
20
_root
20
85
_root
85
64
_root
64
3
_root
3
I don't understand how its able to trace that it is getting the images in the path, yet there are no images to be seen in the gallery!
I found out that my gallery does work (with images and all) when loaded into a movieclip in a blank AS2 file instead of my AS3 site. Am I doomed since even localconnection will not help? -
SetBackground and basic class and method calling stuff
A few days ago I posted a thread about some paint problems. I was then told I was a bad programmer :P So I've read up a little on the subject. Hope this code looks better then the last (event though it's not as "complete" as the last one was).
Now I've got a few new problems though.
// Java Document
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Diamonds2 extends JFrame{
public static void main(String[] args) {
JFrame f = new JFrame("Diamonds 2");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
Container panel = new DiamondsPanel();
f.getContentPane().add(panel, BorderLayout.CENTER);
Container statusPanel = new DiamondsStatusPanel();
f.getContentPane().add(statusPanel, BorderLayout.PAGE_END);
f.setBackground(Color.black);
f.pack();
f.setResizable(false);
f.setVisible(true);
Game d2 = new Game();
d2.game();
class DiamondsPanel extends JPanel {
public static int BallRadius = 16;
public static int posx = 100;
public static int posy = 1;
public DiamondsPanel() {
super();
setOpaque(false); // we don't paint all our bits
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.black));
public Dimension getPreferredSize() {
Dimension panelSize = new Dimension(800, 580);
return panelSize;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
// set color
g.setColor (Color.red);
// paint a filled colored circle
g.fillOval (posx, posy, BallRadius, BallRadius);
public void updateGamePanel() {
posy--;
repaint();
class DiamondsStatusPanel extends JPanel {
public DiamondsStatusPanel() {
super();
setOpaque(false);
setLayout(new FlowLayout());
setBorder(BorderFactory.createLineBorder(Color.black));
public Dimension getPreferredSize() {
Dimension panelSize = new Dimension(800, 20);
return panelSize;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
class Game {
public void game() {
while(0 < 10) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
// System.out.println("Something is happening");
}I can't set the background. I've tried to apply setBackground color to the constructors of each of the panel classes too but that won't work either.
Secondly. How should I do the animation (the red circle should move) now. I knew how to do it before but with this new structure (I found in some swing tutorial) I can't figure it out. I've tried intializing a thread in the class Game since it couldn't be done (according to the compiler) in main. Also, before I could use isVisible() to run the while-loop now that didn't work either and I suppose using 0 < 10 is not a very good way to do it. I also made an update method in the DiamondsPanel class but I don't know how to call it. I understand I'm not doing this as it should be done. I also can't find any place that makes me understand how to do it :P
Any answers will be welcomed with open arms (except mean ones xD).
Thanks alotStyrisen wrote:
I got the background to work.. really don't know what I did. It works thoughGreat
So should the code for animating the ball be inside the DiamondsPanel class? Or where should it be?This is one of the crucial questions when developing an OOP app: what objects to create and what code goes where? I have not done animations, but I believe that there is a 2D forum here in the Sun forums. You might consider asking this over there (but if you do, please be sure to have each thread reference the other so as to avoid duplication of effort from the volunteers who help).
How should the code look like? Should I use runnable? You're definitely going to need to use separate threads. That much I'm sure of.
Should I make an infint loop? No.
Should I use a timer class? Probably.
Is it a bad idea to divide the panels into classes?Not only is it not a bad idea, it may be a good one.
Good luck!
Pete -
I've managed to create several applications in AS 3 without
ever going face to face with packages and classes. I know that
seems hard to believe, but anyway-- now I want to use something in
the corelib and can't figure where I'm supposed to put these files
I've downloaded. I know this is sub basic but would appreciate
help.funonmars,
> Hi David, Thanks for your help.
Sure thing!
> I tried to implement your class structure, then gave it
a rest, then tried
> again today to no avail. Here's a
>
http://henryjones.us/articles/using-the-as3-jpeg-encoder
to the
> JPGencoder demo that got me interested,
Gotcha. Yes, that link helps. I ran through the tutorial
(it's
surprisingly quick!) and met with success. There are various
ways it
*could* be done -- classpath preferences are flexible -- but
here's what I
did.
In the tutorial, just beneath the "First Things First"
heading, Henry
writes:
"Once you have the library, just drop it into your classes
folder and you
are ready to go. Now we can import the JPGEncoder."
The phrase that's open to interpretation is "your classes
folder." For
my development workflow, just personal preference, I keep
third party AS2
and AS3 classes in these folders:
C:\Documents and Settings\<username>\My Documents\My
Projects\ActionScript
2.0 Classes
C:\Documents and Settings\<username>\My Documents\My
Projects\ActionScript
3.0 Classes
These corelib classes are written in AS3, so I put them into
my
ActionScript 3.0 Classes folder. Specifically, I visited the
Google Code
repository linked from Henry's site:
http://code.google.com/p/as3corelib/.
I downloaded corelib-.90.zip and extracted its contents to my
desktop (this
created a folder called corelib-.90 on my desktop). Inside
that folder, I
found a src subfolder. I assumed that meant "source," so I
opened it -- and
sure enough, there was the expected com folder. Inside that
com folder was
an adobe folder, and so on. The JPGEncoder class was
ultimately in this
folder structure: com\adobe\images\JPGEncoder.
I already had a number of third party AS3 classes in my
ActionScript 3.0
Classes folder. Of those, some are located inside a root
folder named net,
some in edu, and some in com. My com folder already had a
number of
subfolders inside it, but not yet adobe; so I dragged the
adobe folder
(along with all of its subfolders) from the corelib-.90
folder on my desktop
into my ActionScript 3.0 Classes\com folder.
From the standpoint of ActionScript 3.0 Classes, then, I had
a
com\adobe\images folder structure that led to the class file
in question.
Here's the relevant classpath setting in my Flash
preferences:
C:\Documents and Settings\<username>\My Documents\My
Projects\ActionScript
3.0 Classes
Based on that classpath setting, here's my import statement:
import com.adobe.images.JPGEncoder;
I can see now, in my previous email, that I accidentally
misled you with
a suggestion for the wrong import path (apologies on that! I
misread your
folder structure). In short, the import path needs to match
the folder
structure of the class files themselves -- from the starting
point of their
parent-most (aka root) folder.
I hope that clears it up!
> Your book arrived today, so maybe that will help me make
> sense of it.
Foundation Flash CS3 for Designers is a FLA-centric book, so
it may not
help you especially with external class files -- but I
certainly hope you
get good use out of it! :) Tom and I tried to cover a broad
range of
topics.
David Stiller
Adobe Community Expert
Dev blog,
http://www.quip.net/blog/
"Luck is the residue of good design." -
Hi,
My SD card reader is not able to write or delete and format sd cards.
WIN7 x64 Home Basic, 0x80070013 when I trying to write or delete files. When I trying to format I can see a message that disk is write protected.
The card is not locked, it's work in Lenovo Idea Pad s10 without any problem, It's also work in my Pentax camera.
I'm almost sure that the problem is software, but can't find solution.
Could Any one help me please?
Laptop is new, 8 days in use.I have network shutdown 2.0.1 installed and the pcns process normally would be abend in 1 or 2 minute's time. What could be the problem ? Consider to upgrade the network shutdown to 2.2.1 version. Could the problem be resolved in new version ? Any precaution needs to be taken when installing the new version ?
Terence. -
i am getting the folloing errors
1067: Implicit coercion of a value of type Number to an unrelated type flash.display:DisplayObject.
1137: Incorrect number of arguments. Expected no more than 1.
my code
cl_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag1);
cl_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
sq_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
sq_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
rec_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
rec_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag1(event:MouseEvent):void {
event.target.startDrag();
if (cl_mc.hitTestObject(mouseX, mouseY, true)) {
message_txt.text = "hit";
else {
message_txt.text = " " ;
rec_mc.x = mouseX;
rec_mc.y = mouseY;
function drag(event:MouseEvent):void {
event.target.startDrag();
function drop(event:MouseEvent):void {
event.target.stopDrag();
can anyone give me a hintNo, but you should be able to search Google using "AS3 collision tutorial" (or similar terms) and find something.
If you can describe what the intent is for what you were trying to accomplish with that code, someone may be able to help resolve things. -
Basic example on Check-boxes using AS3.0 in flash
Hi all,
I need a basic example containing 3 or 5 check-boxes and
should display the names of check-boxes which are
selected in a text-box.
Thanx a lot...Go to the help subject "CheckBox class" and click "view
examples": The example is an exact answer to your question :-) -
Help with designing basic class
Ok, I'm trying to learn java by implementing a Recipe program that I will eventually put up on my web page. My thought was to create a base class called recipe that would essentially be a collection of strings and such. This would then be tied to a database with getFromDatabase() methods etc... However I'm I just want to clarify a few things and make sure I'm headed in the right direction.
I figure I should create my class something like this:
public class Recipe
public String recipeName;
// more strings like serves source, etc...
public int rating; // 1-5
// Ingredient list
// Category list
public String directions;
public Recipe(String recipeName)
this.recipeName = recipeName;
}The above is pretty much what I think is right with what I have so far. For the ingredients, I created a special class called ingredient that looks like:
public class Ingredient
public String qty;
public String amt;
public String desc;
public Ingredient(String quant, String ammount, String description)
this.qty = quant;
this.amt = ammount;
this.desc = description;
public Ingredient()
this.qty = "";
this.amt = "";
this.desc = "";
public String toString()
String tempstring = new String(this.qty + " " + this.amt + " " + this.desc);
return tempstring;
}Then in my recipe class I have:
public List ingredients = Collections.synchronizedList(new LinkedList());and then I have two overloaded addIngredient methods to add an Ingredient object to this list.
So am I on the right track? Should I not even bother with a special class just for ingredients?
Also while I have your attention, if I were to try and get a list of recipes in the database (ie, SELECT recipename FROM recipes;) where should I put this method? Thank you for any help.
-ChrisYou only use get() and set() methods for data you need to change, I didn't think that I had to point that out.
The guy is obviously a beginner, there is no need to saturate him with loads of information that isn't too important to him right now; however, it is good to get him used to the get and set methods for the variables that might need changing, and keeping (most of) the variables private (or protected as the case may be).
Please tell me how setting variables to private does not contribute to information hiding? @_@
Because, the last time I heard, private variables can't be accessed by other classes.
Not only that, the get() and set() methods DO hide information, mainly the inner workings of the code. Consider:
public class Foo {
private int x;
public int getX() {
return x;
public void setX(int anInt){
x = anInt;
public class Bar {
private Foo mFoo = new Foo();
public int process(){
int intialValue = myFoo.getX();
return initialValue * 4;
}And say for whatever reason the way Foo handled the way x was stored had to change:
public class Foo {
public String x = "0"; // x no longer an int
public int getX() {
return Integer.parseInt(x); // convert when needed
public void setX(int anInt){
x = new Integer(anInt).toString(); // convert back
public class Bar {
private Foo mFoo = new Foo();
public int process(){
int intialValue = myFoo.getX(); // none the wiser
return initialValue * 4;
}Note, because of the get and set methods, we didn't have to change Bar. I'd say this was a kind of data hiding, no?
I do know a bit about what I'm saying. I may not be an expert, but what I said was on the wholecorrect, especially considering the OP is a beginner. I never claimed what I told him was the WHOLE of tight encapsulation, but it is a part of it. He doesn't need to know more than what I told him ATM. -
Trying to get AS3 game to keep score of user points and not loop when added to Flash CS4 website
Here is my scripting (seperated into two pages) the font in red is causing the drama so far:
package
import flash.display.Sprite;
public class Easymind extends Sprite //Class Definition
private var timer:Timer;
private var count:uint;
private var prevCount:uint;
private var scoreText:TextField;
private var score:uint;
public function Easymind () //Constructor function
init();
private function init():void
var gift:Gift = new Gift();
gift.x = Math.random() * stage.stageWidth; // is the same as (0 to 0.99) * 550
gift.y = Math.random() * stage.stageHeight; // is the same as (0 to 0.99) * 576
addChild(gift);
scoreText = new TextField();
scoreText.x = 500;
scoreText.y = 10;
scoreText.width = 20;
addChild(scoreText);
stage.addEventListener(MouseEvent.MOUSE_DOWN, kill);
timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, addGifts);
timer.start();
private function addGifts(eTimerEvent)
prevCount = count;
count ++;
for (var i:uint = prevCount; i < count; i++)
var gift:Gift = new Gift();
gift.name = "gift" + i;
gift.x = Math.random() * stage.stageWidth; // is the same as (0 to 0.99) * 550
gift.y = Math.random() * stage.stageHeight; // is the same as (0 to 0.99) * 576
addChild(gift);
private function giftShower()
if (this.numChildren > 250)
timer.stop();
var i:int = this.numChildren;
while(i--)
removeChildAt(i);
addChild(scoreText);
SECOND ActionSript PAGE
package
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.Timer;
import flash.text.*;
public dynamic class Gift extends MovieClip
private var dx:Number = Math.random() * 10; // make gift scurry
private var dy:Number = Math.random() * 10;
public function Gift ()
this.gotoAndStop(Math.ceil(Math.random() * 4));
this.addEventListener(Event.ENTER_FRAME, scurry);
stage.addEventListener(MouseEvent.MOUSE_DOWN, kill);
public function scurry (e:Event)
if (this.x < 0 || this.x > 550)
this.dx *= -1;
if (this.y < 0 || this.y > 576)
this.dy *= -1;
this.x += this.dx; // make gift scurry
this.y += this.dy;
public function die()
this.removeEventListener(Event.ENTER_FRAME, scurry);
parent.removeChild(this);
private function kill (e:MouseEvent):void
if(e.target is Gift)
e.target.die();
score += 10;
scoreText.text = score.toString();and as i mentioned, what's the problem related to that code. the reason i ask is because that code shouldn't compile. specifically,
stage.addEventListener(MouseEvent.MOUSE_DOWN, kill);
timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, addGifts);
timer.start();
is incorrect. and
private function addGifts(eTimerEvent)
prevCount = count;
count ++;
for (var i:uint = prevCount; i < count; i++)
var gift:Gift = new Gift();
gift.name = "gift" + i;
gift.x = Math.random() * stage.stageWidth; // is the same as (0 to 0.99) * 550
gift.y = Math.random() * stage.stageHeight; // is the same as (0 to 0.99) * 576
addChild(gift);
private function giftShower()
is incorrect.
if you don't have a problem with your code compiling, then copy your code and paste it into this forum. -
If I load an external swf:
my_loader.load(new URLRequest("abc.swf"));
addChild(my_loader);
abc.swf starts playing - how do I get it to tell the main movie that it reached the end of its timeline and finished playing? Added bonus: the loaded swf is AS1 and the main movie is AS3. I'm not sure if I could convert the loaded swf to AS3.
Thanks,
SeanI have not used this component but it may work for you:
http://www.jumpeyecomponents.com/Flash-Components/Various/ActionScript-Bridge-91/ -
Basic Classes (Part 2)
I am continuing work with custom classes and OOP. I find that
I can't get eventlisteners and other actions to work unless I use
nested functions (which I know are frowned upon). The following
works correctly but the mouse event listener is an example nested
functions mentioned above. How can I rewrite this without the
nested function that executes the mouse listener? Thanks as always.
package {
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
public class Index extends MovieClip {
public function Index() {
var D:MovieClip = new dMC();
D.x = 100;
D.y = 275;
addChild(D);
var dtext:MovieClip = new dText();
dtext.x = 758;
dtext.y = 384;
var A:MovieClip = new aMC();
A.x = 225;
A.y = 275;
addChild(A);
var W:MovieClip = new wMC();
W.x = 345;
W.y = 275;
addChild(W);
var G:MovieClip = new gMC();
G.x = 470;
G.y = 275;
addChild(G);
var B:MovieClip = new bMC();
B.x = 590;
B.y = 275;
addChild(B);
var L:MovieClip = new lMC();
L.x = 715;
L.y = 275;
addChild(L);
var O:MovieClip = new oMC();
O.x = 820;
O.y = 275;
addChild(O);
var GG:MovieClip = new ggMC();
GG.x = 940;
GG.y = 275;
addChild(GG);
var dSoundReq:URLRequest = new URLRequest("
http://www.clevelandbrowns.cc/audio/dSound.mp3");
var dSound:Sound = new Sound();
var dChannel:SoundChannel = new SoundChannel();
dSound.load(dSoundReq);
D.addEventListener (MouseEvent.MOUSE_OVER, dRoll);
function dRoll (evt:MouseEvent):void {
addChild(dtext);
} // closes Index function
} // closes Index class
} // closes packageI got the problem: my tag list loaded in the whichCode dropdown by the myReadXMLPreferences function has some items beginning with numerical chars (example: 1ACDE) and they are not allowed as tags (I did not remember this limitation). If I choose an alphabetic first char code from the list the script is working.
I think I should filter the list putting a string before any numerical code.
Maybe you are looking for
-
In Iphoto, I would like to produce a bunch of greeting cards with different pics for different people and I would like all these cards to be sent to my home address in one shipment. The way I understand it, I can only prodce one card and order multip
-
I'm trying to clean up my iTunes account and I see several of the same phone or computer listed as authorized users. How do I figure out which are the correct phones/computers?
-
Image name shows instead of image?
i have done several homepages in iWeb. when i tried to make one new today, as usually i dragged the picture from media archive into the page. instad of showing the image on my iweb page, the filename shows. and i cant find whats wrong. i want to use
-
Email settings in J4580 All-In-One Solution Center
Hi there, For some time i use my all-in-one J4580 printer with the Windows Vista application on my computer. But recently i change and did run an upgrade to the Windows 7 application and everything did goes verry well without any problems accept for
-
Distribute with Reference Data
Hi Folks, I have trouble getting the distribution function to work. I want to distribute the plan data to a more granular level (from Product Family to Product Platform) using the previous year's actual sales data. I have setup a plng. function of ty