View Full Version : encapsulation
bestpilotever
08-26-2008, 07:14 PM
If there was a forum for people who are even more of a newb then I am, I'd post this there but there is not. I'm one of those self taught people who really has no idea what he is doing but still I enjoy leaning. Anyhow...
var something_count:Number = 0;
function ballhits(thing){
if (ball1.hitTest(ball2)){
thing_count = 1;
}
}
ballhits(something);
I see that thing just doesn't replace anything within the function with "something". How can I get it do that? Or is there a better way that is just as easy? Thanks for your help in advance.
raydowe
08-26-2008, 08:39 PM
I'm not sure what you are trying to do here. Your explaination at the bottom didn't make much sense to me either.
you are calling ballhits(something), which passes 'something' into the function.
When the function runs, it takes the passed in value ('something') and references it with 'thing', sort of like saying 'thing = something' as soon as the function runs. Whatever 'something' is, it is now called 'thing' inside the function (or could still be called 'something', but that defeats the purpose of passing it in)
then you check to see if two perviously unused variables ('ball1" and "ball2") have hit? If they do, a counter goes up.
Why is anything being passed in if it's not used in the function for anything?
bestpilotever
08-26-2008, 11:31 PM
Thanks for the response ray.
The AS here is fake, my code is way too long to post so I made something up. I could care less what it does, but my problem is as follows: I get the concept that everything inside the function called "thing" is really called "something" when I call it. But when I place a "_" the function is does not work.
In this case the thing_count does not = something_count, perhaps due to the "_". If I want a var to work in the function it would seem that adding a "_" doesn't work. Can I use a "_"? Or perhaps a better way to name a var so that I can keep track of hundreds of them.
raydowe
08-26-2008, 11:43 PM
I see what you are saying now.
Yes, thats correct. the 'thing' in 'thing_count' is not common. In other words, they have no relation. 'thing' is one distinct variable that has been passed in, and 'thing_count' is totally seperate. It has nothing to do with 'thing'.
As far as variable names, the '_' character is just a regular character. It doesn't do anything special for operations, it is just used to name variables like any other letter.
If you do a search through the code i'm sure you will find somewhere, both:
var thing:Something
var thing_count:Something
They have both been declared as seperate variables
This is different than, say:
var thing:Array = New Array(1,2,3);
trace(thing.length);
Hope that explains things without too much rambling. And hope i'm not confused again. :)
raydowe
08-26-2008, 11:45 PM
And just to clarify, passing in 'something' to become 'thing' will not do anything to variables named 'something_whatever'
bestpilotever
08-26-2008, 11:56 PM
Again ray I'm in your debt.
if I have
function namechange(thing){
thing_count;
thing_spin;
thing_hippy;
}
namechange(ball);
Can I make a function to change all of these things knows as "thing" to = ball. Again I don't care what it does (I have that already) I just need to change the name.
raydowe
08-27-2008, 12:02 AM
The one thing you could do is use an object (and boom.... the world of object oriented programming hits you!)
var mything:Object = {name:"My Name", count:5, hippy:WhateverType}
function changeName(thisThing:Object):void
{
trace(thisThing.name); //you will see "My Name"
thisThing.name = "Whatever You Want";
}
changeName(mything);
Hope that helps! :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.