Skip to main content
Inspiring
September 1, 2011
Answered

Reassign class variable to a blank instance of its class

  • September 1, 2011
  • 2 replies
  • 1272 views

When I declare a class variable

private var myVar:MyClass = new MyClass();

It creates a new instance of that class that will trace as [Object MyClass].


However, as soon as I assign it to something

myVar = thisOtherVar;

It will now trace as [Object thisOtherVar].

I need to get it back to where it's just an "empty" blank class instance, if that makes any sense at all.

How do I go about doing that? Thanks!

This topic has been closed for replies.
Correct answer Kenneth Kawamoto

I'm brand new to OOP - I've been watching iTunes U videos and reading books but it's still all pretty abstract in my mind and it's difficult for me to pin point how all that stuff translates to actually writing good code.

Basically, it's an open ended kid's dictionary. You drag the lettes to drop boxes, touch the check to see if it's a word, and then a picture representing the word pops up on the screen.

All of it works except if you try to remove a letter that's already been placed. Originally, I wanted the check button to do a sweep of the dropped letters and see if they had formed a word - but I couldn't figure out a way to do that. So then I started tracking movements of letters in real time, and that's when it got really confusing. I think I just need to start over and try to make the code a bit cleaner. Right now this is what I've got:

if (currentUsedBlank is BlankSquare) {  //if they have removed a letter that has already been dropped
                if (currentUsedBlank != currentBlank) { //and it's been placed back on a different square
                    currentUsedBlank.letter = null;
                }
            }
            else {
                currentUsedBlank.letter = null;
            }
            if (this.dropTarget.parent is BlankSquare && currentBlank.letter == null) {
                SpellingGlobals.lettersInBlanks[SpellingGlobals.lettersInBlanks.length] = this;
                this.x = this.dropTarget.parent.x + 10;
                this.y = this.dropTarget.parent.y - 10;
                var myClass:Class = getDefinitionByName(getQualifiedClassName(MovieClip(this))) as Class;
                var newLetter:MovieClip = new myClass();
                newLetter.x = currentLetter.startPoint.x;   
                newLetter.y = currentLetter.startPoint.y;
                newLetter.letter = currentLetter.letter;
                newLetter.reference = currentLetter.reference;
                newLetter.startPoint.x = currentLetter.startPoint.x;
                newLetter.startPoint.y = currentLetter.startPoint.y;
                newLetter.isVowel = currentLetter.isVowel;
                currentBlank.letter = currentLetter.letter;
                if (SpellingGlobals.isCaps == true) {
                    newLetter.txt.text.toUpperCase();
                    if (SpellingGlobals.isColor == true) {
                        if (newLetter.isVowel) {
                            newLetter.txt.textColor = SpellingGlobals.colorVowel;
                        }
                        else {
                            newLetter.txt.textColor = SpellingGlobals.colorCon;
                        }
                    }
                }
                MovieClip(root).addChild(newLetter);
                SpellingGlobals.copiedLetters[SpellingGlobals.copiedLetters.length] = newLetter;
                currentBlank.alpha = 0;
            }
            else {
                this.x = MovieClip(this).startPoint.x;
                this.y = MovieClip(this).startPoint.y;
            }
            this.stopDrag();
        }

I feel you are overcomplicating things How I'd do is to have two classes: Letter class and (for want of a better word) Word class, both extends Sprite.

Letter class dispatches events on mouse press (start drag) and on mouse release (stop drag).

Word class listens to above events. On release (drop) event, if the Letter is dropped in one of its empty slots populate the array of letters. On press event (pick up = start drag) if the Letter was in one of the slots remove it from the array of letters.

When the "look up" button is pressed you use the array of letters in the Word class to see if it forms a word. Simple

2 replies

AmbariAuthor
Inspiring
September 2, 2011

I've tried all those things...I just can't get it work.

And Andrei - lol, I have no doubt there's a flaw in my approach.

I have just discovered Display Object Containers - I'd never heard of this before.

Is this an application for that class? Could I make each blank square a Display Object Container and check that way which letter class each one contains? Is that possible? (I've read a bit about display object containers but it's obviously pretty abstract so I'm wondering if I'm on the right track or not.)

Thanks

(PS - sorry I didn't have more "helpful answers" to give out!)

Inspiring
September 2, 2011

Look, as much as I want to help, still, without seeing your code and having a better idea how you are attempting to implement this use case, I don't see a way to come up with a valuable suggestion.

In a nutshell, there is a great concept on programming/application development that states that information, presentation layer and logic must be separated. in your case it translates, partially, into the not expecting a great solution if what you display somehow can represent anything but visuals. in other words, when you place letters into blanks or not will not miraculously translate into application being aware of where things are and how they should behave - it is your task to create this awareness.

DisaplyObjectContainer is a super class of several other very well familiar Classes as MovieClip, Sprite, etc. So, MovieClip is a DisplayObjectContainer as well as is Sprite. But, I guess, this doesn't help much. Again, because one uses DisplayObjectContainer properties and methods, it doesn't mean things will work.

It loos like you need to keep references to the letters in some structure (Array, Object, Vector, Dictionary, special custom Class - choice is yours) and have code record letters states/positions/placements on display lists (where they are - in blank spaces, outside, etc.) and then act upon this information as user interacts with the UI.

In any case, it will take forever to explain things. Forum is more suitable for short focused communications based on particular code snippets.

With that said, the issue (at least in my eyes), as far as this thread is concerned, is that there is no clarity in what you are trying to accomplish on a bigger scale. Is it scrabble? Is it hangman? What is the point of moving of letters around? What is the final result of user interacting with you application?

Only you can answer these questions - it is your vision after all and we are here to follow it.

AmbariAuthor
Inspiring
September 2, 2011

I'm sorry. I didn't mean to be confusing. I'm just confused.

Thanks for all your help.

kglad
Community Expert
Community Expert
September 1, 2011

you should get a compiler error if you try to assign it a different class instance and you shouldn't assign to a different instance.  if you wanted to create a variable to store any of several MyClass instances you should use:

private var myVar:MyClass;

AmbariAuthor
Inspiring
September 1, 2011

Right, which is what I'm doing. Let me be more specific I think I was too abstract.

I have 34 movie clips on the stage. 26 are assigned to the class Letters. 8 are assigned to the class BlankSpace.

Because there is no way to do a hitTest that returns the value of an object, I have to keep track in real time of which letters are assigned to which blank spaces.

I keep track of which letters are assigned by creating a variable inside the class Letters of the class BlankSpace. Whenever a letter is dropped over the space, it is recorded inside this variable.

However, the problem comes when I try to keep track of letters that are REMOVED from each BlankSpace.

I have tried this:

if (currentUsedBlank is BlankSquare) {
                if (currentUsedBlank != currentBlank) {
                    currentUsedBlank = null;
                }
            }
            else {
                currentUsedBlank = null;
            }

Which basically says "if a letter that was once on a blank square is dragged onto a different blank square, erase the data that was in the previous blank square."

The problem is that "null" is not what I need to do. I just need it to be a completely empty class definition again. If I assign the value "null" I can't do anything else with that square any more. I just need it to be new. I can't redeclare the variable; I get a compiler warning.

Does that make any sense?

Kenneth Kawamoto
Community Expert
Community Expert
September 1, 2011

I'm not certain if I understood your description but currentUsedBlank = new BlankSquare(); is probably what you're after. However this is not the way to do - why not just use String to record what is dropped/removed?