Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you either did something wrong when you copied the code into SO or you're not placing the trace statements correctly. You need to place the <code>trace("This is TestOne");</code> inside a constructor in TestOne.as, like this: </p> <pre><code>public function TestOne() { trace("This is TestOne"); } </code></pre> <p>The same goes for the code inside the Init class, which now reads:</p> <pre><code>trace("This is Main Class"); var testOne : TestOne = new TestOne; </code></pre> <p>but should be(note the bracers after new TestOne):</p> <pre><code>public function Init() { trace("This is Main Class"); var testOne : TestOne = new TestOne(); } </code></pre> <p>What happens when you run your SWF, is that the constructor of the class <code>Init</code> will:</p> <p>1) Trace "This is Main Class" to your console.</p> <p>2) It will construct a new object(thus the name constructor) by calling the constructor of the class <code>TestOne</code>.</p> <p>If you were to add this line to the end of the constructor in the class <code>Init</code>:</p> <pre><code>testOne.testing(); </code></pre> <p>you should see this in the console: "This is the testing method".</p> <p>If you would now comment out the line: <code>var testOne : TestOne = new TestOne();</code> and run the SWF again, you'll get an error telling you something is null. This is because you attempt to call the method <code>testing</code> on an object that does not exist.</p> <p>I do realize this is primarily fixing some coding errors of yours, and not so much helping you understand OOP. But hopefully you can pick up some help regarding object construction. I see some answers already mention key OOP principle that you really should look into.</p> <p>Edit:</p> <p>Remember that duplicating code is never a good thing, so if you find that all of the classes TestOne - TestFive contain the same code, except for some minor detail. You should probably change them into one class.</p> <p>In your case you could for example change TestOne so that the constructor accepts a <code>String</code>, and then in your <code>testing</code> function you could just trace that <code>String</code>. By changing your TestOne class into the following you effectively get rid of 4 other classes. You also encapsulate a <code>String</code> inside of the class TestOne. </p> <pre><code>package classes { import flash.display.MovieClip; public class TestOne extends MovieClip { private var message : String; public function TestOne(myMessage:String) { message = myMessage; trace("This is TestOne"); } public function testing() : String { return message; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload