Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

890652 Posts in 33509 Topics- by 24748 Members - Latest Member: CherrySlug

June 17, 2013, 09:25:47 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Inheritance Headaches [Solved]
Pages: [1]
Print
Author Topic: Inheritance Headaches [Solved]  (Read 267 times)
Sean A.
Level 7
**



View Profile Email
« on: August 11, 2012, 12:32:15 PM »

So I've been having some trouble getting something to work. The problem is much bigger than what I'm showing you but I've located the source of the problem. I'm getting strange results when trying to override a function here is the code for the two classes in question:

Code:
package
{
public class ScaleableObject extends GameObject
{
public var percentX:Number;
public var percentY:Number;
public var percentWidth:Number;
public var percentHeight:Number;

public function ScaleableObject($percentX:Number, $percentY:Number, $percentWidth:Number, $percentHeight:Number)
{
super();
percentX = $percentX;
percentY = $percentY;
percentWidth = $percentWidth;
percentHeight = $percentHeight;
}

public function screenChange($width:Number, $height:Number):void
{
x = percentX*$width;
y = percentY*$height;
width = percentWidth*$width;
height = percentHeight*$height;
trace("----->",$width+"*"+percentWidth+"="+width,$height+"*"+percentHeight+"="+height);
}
}
}

Code:
package ui
{
public class MainMenu extends ScaleableObject
{
public function MainMenu($percentX:Number, $percentY:Number, $percentWidth:Number, $percentHeight:Number)
{
super($percentX, $percentY, $percentWidth, $percentHeight);
}

override public function screenChange($width:Number, $height:Number):void
{
trace($width+"*"+percentWidth+"="+width,$height+"*"+percentHeight+"="+height);
super.screenChange($width,$height);
trace($width+"*"+percentWidth+"="+width,$height+"*"+percentHeight+"="+height);
}
}
}

The only other thing that happens is that a mainMenu object is created and then everytime there is a resize event it calls screenChange()

Here are the results of the traces
Code:
320*1=0 480*1=0
-----> 320*1=0 480*1=0
320*1=0 480*1=0
The the first one is called before the super.screenChange() the second is inside the super classes method and the third is after the call.

The second one is what blows my mind. Look at the results and then look at where the trace came from. I just printed the two inputs and the output and apparently it doesn't know how to multiply properly? I have no idea whats going on, any help would be fantastic. Also if anyone needs any clarification just ask.
« Last Edit: August 12, 2012, 10:13:22 AM by Sean A. » Logged
bateleur
Level 10
*****



View Profile
« Reply #1 on: August 12, 2012, 04:34:37 AM »

I can't tell what's going on from what you have here, but here are a few testing tips...

As a test, do the multiplication using all local variables. Something like this:

Code:
var iw:Number = 320;
var pw:Number = 1;
var w:Number = iw * pw;
trace("iw (" + iw + ") * pw (" + pw + ") = w (" + w + ")");

Don't write weird features in your trace statements. Use only strings or things which type convert to strings. Don't use '$' in variable names - that's reserved for use by autogenerated code.

If the above test code works, try that trace statement in your original examples. If you still see the problem in that case it might be worth checking the types of all the variables involved, since if they're not all Numbers that could be causing problems.
Logged

Sean A.
Level 7
**



View Profile Email
« Reply #2 on: August 12, 2012, 09:59:04 AM »

I was only using the dollar sign for variables passed in arguments, I had no idea that it was reserved. Thanks for the tip. Using local variables worked thank god.

So I looked into it a bit and the reason it wouldn't work is because scaleable object is extending Sprite, and the width and height properties of a height arent supposed to be set. It is supposed to decide what they are based on the children. Instead it sets the scale values instead when you try to set it. So when I was working with all my other classes main menu had children and the values getting passed around were insane like 100405.685 for one of the widths and it has to do the with the scaling thing, not to mention the fact that every time i added children to anything it would fuck up the width values I had set earlier. So the solution is to just use my own width and height variables and pass those instead.

Thanks for all of the help Beer!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic