I am trying to make a simple as2 shooter but when I try to shoot an enemy the bullet just go thru it and doesn't remove the enemy. I tried to put
if (hitTest(_root.vihollinen)==true
){
_root.vihollinen.remove();
this.removeMovieClip();
}
but nothing happens
Most of the code is just copy/paste because I don't know much about coding but I'm trying to learn!
https://www.dropbox.com/s/58u34tbeve6oile/game.zip
The most significant issue is that your enemy needs an Instance Name in order for the code to understand it. Simply click on your enemy movieclip, and add vihollinen to the Instance Name field at the top.
Next, your bullet code is close, but needs some adjustments. Here is your code:
_root["bullet" + bulletsFired].onEnterFrame = function(){
this._x += this.xmov;
this._y += this.ymov;
if (hitTest(_root.vihollinen)==true){
_root.vihollinen.remove();
this.removeMovieClip();
}
};
If you're ever using an onEnterFrame handler like that with a function, it's good practice to always refer to the current object with this, like so: if(this.hitTest(_root.vihollinen) == true){
remove() is not an ActionScript2 function. Try using unloadMovie() instead, like this: _root.vihollinen.unloadMovie()
Changing those three things will make your code function. Be sure that you try to go through each part of your code and understand it to the best of your ability - it'll make things much easier in the long run, even if it takes a long time to figure out why each part is there!
Related
I'm trying to refactor a small amount of legacy non-functional-totally-procedural-old-school-"is this FORTRAN?!" code, and I'd like some input.
Since Swift 3 is getting rid of the "confusing" ++ prefix/postfix operator, along with C-style for loops, I've been hard at work updating my gross Swift 2.0 (and really gross C and Objective-C) files, and for the most part, it hasn't been too bad. However, I've run into one of those pesky do...while loops that I probably wrote after a night of heavy drinking.
Here's a slimmed down version of the original code:
BSDPath *startPath = &ctx->paths[startIdx];
BSDPath *endPath = &ctx->paths[endIdx];
BSDPath *currentPath = startPath;
do {
if (flags & CurveElement) {
// ... Some code here ...
}
} while (currentPath++ != endPath);
I've Swiftified some stuff, and now I'm here:
let startPath = ctx.paths[index]
let endPath = ctx.paths[endIndex]
var currentPath = startPath
repeat {
if flags.contains(.Curve) {
// ... Some code here ...
}
index += 1
currentPath = ctx.paths[index]
} while currentPath != endPath
As you can C, I've Swiftly converted most of it to the new nice and clean Apple of my eye. However, I can't help but wonder if there's an even cleaner way of writing this — that is, without totally throwing out the bath water and giving in to the for...in siren song. To be honest, I'm not even sure if that code runs correctly. I've never really been able to wrap my head around the control flow of the do...while statement. I mean, I get how it works, but it just seems backwards to me. But I digress.
I suppose the reason I want to know the "cleanest" way to rewrite my old stuff is that I just kind of miss being able wrap assignment statements in parentheses to kill two bytes with one var. I totally agree that the ++ operator is counterintuitive, but for all it's jankiness, it certainly did take a little of the tedium out of typing into a code editor all day. But I digress (again?!).
Anyway, that's all I've got. I would love to know of any timesavers I'm unaware of, but any thoughts, ideas, or comments are welcome and greatly appreciated. Thanks!
Sorry in advance, this is an extremely noobie question (but i'm just getting into NGUI with unityscript and can't find many answers/tutorials/docs).. Also my untiyscript skills are sub-par.
I have a TCG/Playing card game object with some basic RPG stats (strength, dexterity) that currently display on the card in GUIlabel and trying to convert this to NGUI. I'm adding a UILabel as a child to the card (which contains the stats script)
Looking for some advice on going about this, the only way I've even remotely gotten something to display correctly is, unfortunately I have to attach the stats script to the label too:
var strLbl : UILabel;
function Start() {
var strLbl = GetComponent(UILabel);
}
function OnGUI() {
strLbl.text = strength.ToString();
}
This is throwing numberous 'nullreferenceexception: object reference not set to an instance of an object (for the stats script)
Do I need to make a separate label for each stat or is there a way
to aggregate it into one label? (seems when I try to add strength
,then dexterity it overrides it)
is OnGUI the correct course for NGUI or is there a more efficient
function?
Is this script attached to the object that the UILabel is on? You should do a check for
if(strLbl != null)
strLbl.text = strength.ToString();
You could aggregate them into one label (though if individual stats update I would advise against it), assuming you want each stat on a newline then your next would be: strLbl.text += "\n" + dexterity.ToString()
No need to use OnGUI with NGUI. Especially not for setting things. You probably want to do this whole stage in Start() and have another method called for updating the label.
Another one noobie question.
I have movieclip spaceship_mc on Main Timelime.
Its instance name is spaceship1_mc (added from library manually).
In Library this symbol has 2 layers: Objects layer and Action layer.
I put inside Action layer such simple code:
var spaceship1_mc:MovieClip = new MovieClip;
spaceship1_mc.blendMode = BlendMode.SCREEN;
spaceship1_mc.scaleX = 2;
spaceship1_mc.scaleY= 2;
I suppose, that at run time this code must work automatically, and all those parameters will be set at the very beginning.
However, nothing changes. As if this code doesn't work.
QUESTION1: Please, tell me what is wrong?
Maybe I should use some more complicated dot-syntax?
QUESTION2: What is the name of Main Time Line?
I tried to use stage, root, MainTimeLine, in the code above, but it doesn't work.
Using google I've found one decision.
Perhaps, It is somewhat redundant and (..Hey...WTF? It's helicopter at the skyline, dudes!...Cool...)... Okay.
And here it is:
var ship1:MovieClip = parent.getChildByName("spaceship1_mc") as MovieClip;
ship1.blendMode = BlendMode.SCREEN;
ship1.scaleX = 2;
ship1.scaleY= 2;
Well... I suppose it is somewhat too indirect.
So we must read the name of some Instance and determine it as variable. I guess there must be some more laconic way.
I'm writing a simple QR code generator (just for fun and to learn some Obj-C), and I'm working on tracing the outline of connected "modules" (i.e. the black squares that make up a QR code). This is in order to have nicer vector output than simply making a bunch of rects for each module.
Long story short, my outline-tracing code works - BUT ONLY if I make sure to call NSLog in a specific place! If I remove the NSLog-call, the code loops! I'm literally doing nothing but logging. And it doesn't matter what I log; I just have to call NSLog or things break.
The tracing algorithm is simple enough: Go clockwise around the shape of connected modules. When you hit a corner, turn right until you're back to following the outline of the shape. Stop when you reach the starting point again. The shape can have two modules that share a corner-point. The tracing-loop will thus hit that point twice. This is expected, and the code handles it correctly - if I call NSLog.
Otherwise, the code will say that a certain point is a corner the first time it sees it, and not a corner the second time, which causes the tracing to loop around. Detecting if something's a corner-point is not dependent on anything except the x and the y coordinates of the point and an array of module objects - but neither the modules nor the array changes while the tracing is going on, so given the same x,y you should always get the same result. And it does – if I call NSLog.
Without NSLog, the coordinates – e.g. (10,9) – is corner on moment, and a moment later (10,9) is suddenly not a identified as a corner. But with an NSLog-call, (10,9) is correctly seen as a corner-point every time.
Again: I change absolutely nothing; I just log something - anything! And suddenly it works. It's like it's saying that 2 == 2 is true or false, unless I tell it to log 2 and 2, in which case 2 == 2 is always true, as it should be.
Here's the flaky code. It's hard to understand out of context, but there's a lot of context, so I hope this is enough. Everything is integers (no fuzzy floating point values).
do { // start going around the shape
// If this isn't here or simply commented out, the code loops.
NSLog(#"foobar"); // doesn't matter what I log - I just need to log something
// Branch: Is current x,y a corner-point? This should
// always return the same result given the same X and Y
// values, but it only does if NSLog is there!
if( [self cornerAtX:x Y:y] ) {
// add the point to the path
[path addPoint:NSMakePoint(x, y)];
// rotate the direction clockwise, until
// the direction is following the edge of the
// the shape again.
do {
dt = dx;
dx = -dy;
dy = dt;
} while( ![self boundaryFromX:x Y:y inDirectionX:dx Y:dy] );
}
// continue along direction
x += dx;
y += dy;
} while( !(sx == x && sy == y) ); // we're back to the start of the shape, so stop
If anyone can tell me why NSLog can make code work (or rather: Why not using NSLog makes working code break), I'd be happy to hear it! I hope someone can make sense of it.
Make sure cornerAtX:Y: always returns something—i.e., that there's no code path that fails to return a value.
Otherwise, it may very well “return” whatever the last function you called returns, in which case calling NSLog (which doesn't return a value, but may ultimately call a function that does) causes it to “return” something different, which may always be something that's considered true.
The compiler should warn you if you fail to return a value from a function or method that you declared as doing so. You should listen to it. Turn on all the warnings you can get away with and fix all of them.
You should also turn on the static analyzer (also included in that post), as it, too, may tell you about this bug, and if it does, it will tell you step-by-step how it's happening.
There's not much to go on here, but I'd guess that it's either an uninitialized variable or some sort of memory stomping. NSLog probably uses both stack and heap memory, so it could affect those.
Have you tried replacing NSLog with some other meaningless operation? If that will also work then I suppose problem is linked to [self cornerAtX: x Y: y].
Another possibility is that the problem is time-related. NSLog takes time to execute, so if QR code is loaded in another thread you can see this kind of behavior.
first post here so sorry for the length of it. I've been lurking and learning a lot so far but now I have to step in and ask a question. I have read numerous posts here as advised in the FAQs, but I couldn’t find exactly the answer I’m looking for.
Before anything else, let me just say that I'm a total beginner in programming (let alone Objective-C) so please excuse me for any misuse of the terminology. Same goes for any funny english as english not my native language.
I'm building an unit conversion application with a main window containing (among other stuff) two popUpButtons. I'm using indexOfSelectedItem from both popUpButtons in order to calculate a float value (I'm getting the indexes initially in the AwakeFromNib and later in the pop up buttons controller method, when the user change selection).
My problem consists of two parts: first, the code for calculation of that float is pretty massive as I'm comparing every combination of the two indexes of selected items. And second, I would need to have the calculated float value available immediately after launch as the user might want to perform a conversion before using any of the window popUpButtons (otherwise I would put the calculation code in a -(IBAction) method).
So, I'm trying with the following code for calculation of the float value:
#interface MyClass: NSObject
float calculatedFloat;
-(void)setCalculatedFloat:(float)calcFl;
-(float)calculatedFloat;
#implementation MyClass
-(void)setCalculatedFloat:(float)calcFl {
calcFl = 1.0; // I'm simplifying, this is where I'd like to perform calculation
}
-(float)calculatedFloat {
return calculatedFloat;
}
Now, for the first part of my problem, when I use the calculatedFloat in another method, say:
-(void)printIt {
NSLog(#"Calculated float equals: %.2f", calulatedFloat);
}
all I receive in Debugger is 0.00.
First question would be: if this is not working, how do I properly access this value from within another method?
For the second part of the problem, I'm using -(void)AwakeFromNib; to set up popUpButtons etc. right after the launch but I really wouldn't like to put all of the float calculation code in it only to repeat it somewhere else later.
So the second question would be: is this even possible what I'm trying to achieve? Further more, do I need to move this calculation code to another class? If so, how can I make that other class aware of the indexOfSelectedItem from a popUpButton?
Sorry for the lengthy post and possibly confusing and silly questions. I hope you didn't have to cringe your teeth too much while reading! :)
Thanks!
-(void)setCalculatedFloat:(float)calcFl {
calcFl = 1.0; // I'm simplifying, this is where I'd like to perform calculation
}
This doesn't show up when you print it later because you assigned to the variable holding the new value, not the variable for the value of the property. You need to assign to your calulatedFloat instance variable.
(You typo'ed that variable name, BTW.)
You should move the calculating into another method, and send yourself that message from awakeFromNib and from anywhere that needs to cause recalculation. That method should call setCalculatedFloat: with the calculated value—i.e., setCalculatedFloat: should be just a simple setter. Once you make that change, you could replace your custom accessors with a #synthesize statement and let the compiler write the accessors for you.
My problem consists of two parts: first, the code for calculation of that float is pretty massive as I'm comparing every combination of the two indexes of selected items.
You might see whether you can create custom objects to set as the menu items' representedObject properties, in order to cut out this massive comparison tree. It's hard to be more specific about this without knowing what your comparison tree does.