Turn-based Game Loop - actionscript

I am having a lot of headache trying to make a game loop in Actionscript. Searched a lot and could not to find the answer. My idea is:
When de game starts, it defined the units sequence of attack, based in their speed (each player can have until 8 units, of any kind). I store this sequence into an Array. I did create the engine in Javascript returning the values in console.log... and works fine... but transporting it to AS3, the things aren't working as I thought.
So... I have the sequence... and now I do the loop
This is my logic (not using any specific language):
for (i = 0; i < sequence.length; i++) {
isHitting = sequence[i]; // first unit from list is the hitter
isDefending = mostPowerfulEnemy(); // method to check who will be attacked
// here is the problem!!!!
isHitting.moveTo(isDefending); // method to move the MC near the target
var kills = isHitting.hit(isDefending); // calc the damage and return the kills
if (isDefending.amount <= 0) {
// remove the MC from Stage and the sequence list
continue; //to move to the next hitter in sequence
} else {
isDefending.amount -= kills;
continue; //to move to the next hitter in sequence
}
}
The problem is: all units are moving at same time!
I've learning about Events and the method addEventListener() sounds like the best option, but i have to call a function, right? I do it... so the unit move to the point... hit... and stop... I need a way to say: "hey, this unit already did his move, did stop and did hit the target, you can now continue the loop" (since I cant return a continue, ofc)
This is what I want:
Some suggestion?
Sorry my bad english.

Related

In Lua, using a boolean variable from another script in the same project ends up with nill value error

This code is for a modding engine, Unitale base on Unity Written in Lua
So I am trying to use a Boolean Variable in my script poseur.lua, so when certain conditions are met so I can pass it to the other script encounter.lua, where a engine Predefined functions is being uses to make actions happens base on the occurring moment.
I tried to read the engine documentation multiple times, follow the exact syntax of Lua's fonction like GetVar(), SetVar(), SetGobal(),GetGlobal().
Searching and google thing about the Language, post on the subreddit and Game Exchange and tried to solve it by myself for hours... I just can't do it and I can't understand why ?
I will show parts of my codes for each.
poseur:
-- A basic monster script skeleton you can copy and modify for your own creations.
comments = {"Smells like the work\rof an enemy stand.",
"Nidhogg_Warrior is posing like his\rlife depends on it.",
"Nidhogg_Warrior's limbs shouldn't\rbe moving in this way."}
commands = {"GREET", "JUMP", "FLIRT", "CRINGE"}
EndDialougue = {" ! ! !","ouiii"}
sprite = "poseur" --Always PNG. Extension is added automatically.
name = "Nidhogg_Warrior"
hp = 99
atk = 1
def = 1
check = "The Nidhogg_Warrior is\rsearching for the Nidhogg"
dialogbubble = "rightlarge" -- See documentation for what bubbles you have available.
canspare = false
cancheck = true
GreetCounter = 5
Berserk = false
encounter:
-- A basic encounter script skeleton you can copy and modify for your own creations.
encountertext = "Nidhogg_Warrior is\rrunning frantically"
nextwaves = {"bullettest_chaserorb"}
wavetimer = 5.0
arenasize = {155, 130}
music = "musAncientGuardian"
enemies = {"poseur"}
require("Monsters.poseur")
enemypositions = {{0, 0}}
-- A custom list with attacks to choose from.
-- Actual selection happens in EnemyDialogueEnding().
-- Put here in case you want to use it.
possible_attacks = {"bullettest_bouncy", "bullettest_chaserorb", "bullettest_touhou"}
function EncounterStarting()
-- If you want to change the game state immediately, this is the place.
Player.lv = 20
Player.hp = 99
Player.name = "Teemies"
poseur.GetVar("Berserk")
end
Thank you for reading.
The answer to my problem was to use SetGobal(), GetGobal().
For some reasons my previous attempt to simply use SetGobal()Resulted in nil value despite writing it like that SetGobal("Berserk",true) gave me a nill value error, as soon as I launch the game.
But I still used them wrong. First I needed to put it SetGobal() at the end of the condition instead of at the start of the the poseur.lua script because the change of value... for some reasons was being overwritten by it first installment.
And to test the variable in the function in my encounter.lua, I needed to write it like that
function EnemyDialogueStarting()
-- Good location for setting monster dialogue depending on how the battle is going.
if GetGlobal("Jimmies") == true then
TEEEST()
end
end
Also any tips an suggestions are still welcome !
Well firstly, in lua simple values like bool and number are copied on assignment:
global={}
a=2
global.a=a--this is a copy
a=4--this change won't affect value in table
print(global.a)--2
print(a)--4
Secondly,
SetGobal and the other mentioned functions are not part of lua language, they must be related to your engine. Probably, they use word 'Global' not as lua 'global' but in a sense defined by engine.
Depending on the engine specifics these functions might as well do a deep copy of any variable they're given (or might as well not work with complicated objects).

Can I get a calibrated image and skeleton from the Official Kinect SDK at the same time?

Basically, I just want those three things.
Color, Depth, and Skeleton. But I also want the depth and color lined up.
I know that in the official examples, there's GreenScreen.cpp, which is an example of depth/color cut-outs, but no skeleton. There's also SkeletonBasics which has a skeleton, but no image.
I've tried learning from both of them and implementing them together, but I never get a callback for when the skeleton is ready (this: m_hNextSkeletonEvent).
SkeletonBasics.cpp uses this:
hEvents[0] = m_hNextSkeletonEvent;
// Check to see if we have either a message (by passing in QS_ALLEVENTS)
// Or a Kinect event (hEvents)
// Update() will check for Kinect events individually, in case more than one are signalled
DWORD dwEvent = MsgWaitForMultipleObjects(eventCount, hEvents, FALSE, INFINITE, QS_ALLINPUT);
// Check if this is an event we're waiting on and not a timeout or message
if (WAIT_OBJECT_0 == dwEvent)
{
Update();
}
And GreenScreen.cpp uses this:
hEvents[0] = m_hNextDepthFrameEvent;
hEvents[1] = m_hNextColorFrameEvent;
// Check to see if we have either a message (by passing in QS_ALLINPUT)
// Or a Kinect event (hEvents)
// Update() will check for Kinect events individually, in case more than one are signalled
DWORD dwEvent = MsgWaitForMultipleObjects(eventCount, hEvents, FALSE, INFINITE, QS_ALLINPUT);
// Check if this is an event we're waiting on and not a timeout or message
if (WAIT_OBJECT_0 == dwEvent || WAIT_OBJECT_0 + 1 == dwEvent)
{
Update();
}
But, like I said, combining them yields no
if ( WAIT_OBJECT_0 == WaitForSingleObject(m_hNextSkeletonEvent, 0) )
happening. (The KinectBridgeWithOpenCVBasics-D2D also does Skeleton, Color, and Depth, but still is unaligned (like this example I found in a search).
Do you know why the event is not firing? Or perhaps do you know of an example somewhere that has all of these abilities? I've searched high and low, and I've tried tons of different things messing with these examples, but I just cant seem to get it.
Thanks for your help.
EDIT:
At the end of KinectBridgeWithOpenCVBasics, I'm left with a m_colorMat and an m_depthMat. Even if at that point I could align the depth to color, I think that would work out just fine.

ActionScript Unexpected Curly Braces/Semicolon?

I'm editing an ActionScript file and I've run into an issue.
When I put the following, everything is fine.
if (x=x) {
//blah
}
If I put this, it says unexpected ; for one line and } for the another:
for (x=x) {
//blah
}
Same with when I put this:
while (x=x) {
//blah
}
Of course I only put those there as examples to test it, because I thought something was wrong with my code. Is ActionScript, in this part of my file, only allowing IF statements or what? I need to do the same long series of steps to two different strings, but I don't want to put the code in there twice. Do I have to make a function?
Read up on the looping syntax here.
The For loop doesn't take a boolean (true/false), it needs a counter, a boolean check for the limit and an increment.
i.e.
for (counter; condition; action){
statements;
}
I've never used action script but I would suggest trying this with
x==x
Since once = is assignment, not a comparison.
if the for loop still does not function try
for(;x==x;){
}
the semicolons tell it that you want to only use the second statement in the for loop declaration, the condition; since for loops use three statements,
for (variable; condition; iterative action)
by placing semicolons before and after x==x you specify only the condition, which seems to be what you're trying to do.
Turns out using any IF or WHILE statements caused the error no matter what was inside.
I was able to accomplish what I wanted by making another function and sending each string though those.
Appreciate the help, voted up on both of y'all.
you have to write it like this:
if(a==x){
// do that
}
for (x=0; x<maxloops; x++){
// do that
}
while(a==x){
}
The = symbol is used to define values to varialbes, while == has to be used when you comparing / checking (i.e. whether this is equal to that). This both applies to IF and WHILE
the FOR LOOP. Let's say that you want to execute the action "do that" 10 times. then you write
for (x=0; x<10; x++){
// do that
}
the first part x=0 is the definition of the counting variable and its initial value
the second part is the condition (run the loop as long as x is less than 10)
the third part is the stepper. (how the counter will raise its value in each loop). x++ is a short way to write x = x +1;

What am i doing wrong with random?

I have some code that uses a random number to determine whether an object is special or not. I use this code on each of 4 objects that are reset every so often.
Random rand = new Random();
int i = rand.Next(1, 25);
if (i == 1)
{
thiss.typer = "boulder";
thiss.texture = Content.Load<Texture2D>("rock");
}
else if (i == 2)
{
thiss.typer = "ice";
thiss.texture = Content.Load<Texture2D>("ice");
}
else if (i == 3)
{
thiss.typer = "bomb";
thiss.texture = Content.Load<Texture2D>("bomb");
}
else
thiss.typer = "normal";
But every time I execute this code, when it creates the 4 objects, when it makes one of them special, it makes all of them special for some reason. Is there something wrong with this code, or will i have to show more of my code to shed some light on it?
I can think of two reasons why this could happen.
Firstly, maybe you accidentally share a reference between your objects, so that thiss.typer refers to the same object in all 4 cases.
Secondly, and more likely, the Random-objects you create share the same seed, so that rand.Next returns the same number in all 4 instances.
When you do new Random() without an argument, the seed is based on the current system time. If you do it multiple times in quick succession, the seeds might be the same.
You could try using the same Random object, instead of creating a new one each time it is used. This way you also avoid creating lots of objects unneccessarily.

Adding the enemies

OK, so I have a laser gun and it is shooting lasers (well duh) called laser_mc and I am putting in the enemies now. There is one problem though. When I add the enemies named bad they get added, removed and then reappear somewhere else.
Here is my code. What did I do wrong?
var badadd:bad; badadd = new bad()
addEventListener(Event.ENTER_FRAME, createbad);
function createbad(event:Event):void {
addChild(badadd);
badadd.x = Math.random()*stage.width;
badadd.y= Math.random()*stage.height;
}
addEventListener(Event.ENTER_FRAME, removebad);
function removebad(event:Event):void {
if (laser_mc.hitTestObject(badadd)) {
removeChild(badadd);
}
}
They get removed and placed elsewhere because you are using an enter_frame loop here. Every single time a frame ticks off your program adds the same enemy at a random location. So it adds at a random place, removes it, then adds it at a random place all over again.
You might want to try something like this:
Set up a for loop and fill an array with enemies. Declare the array as a class property\, EnemyArray. Like (pseudocode):
for i = 1 to 10
var tempEnemy = new Enemy()
EnemyArray[i].push(tempEnemy) // put the enemy in the array
Now when you need to add an enemy - it's already been instantiate so you just need to go:
addChild(tempEnemy[index]);
Now you can cycle through the array for hit testing, etc.
Let me know if this is too conceptual and I'll write the code out a bit more.

Resources