Hittesting against a level building array - arrays

I'm building a top down (birds eye view) game in flash, a fairly simple concept, a la early Legend of Zelda or Final Fantasy games. I have the level built and the character added and moveable, with a scrolling camera, yet one problem vexes me. Hittesting.
The level is created using an array- a simple [1,1,1]; array, where it checks to see if a value in the array matches, then creates a simple square Sprite and adds it to a larger Sprite that holds every item. Finally, the player is then added to the stage and can be controlled using the arrow keys. I'm using this as a movement system:
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkUp);
stage.addEventListener(Event.ENTER_FRAME, update);
function checkDown(e:KeyboardEvent):void {
switch (e.keyCode) {
case 65 :
leftDown = true;
break;
case 37 :
leftDown = true;
break;
case 68 :
rightDown = true;
break;
case 39 :
rightDown = true;
break;
case 87 :
upDown = true;
break;
case 38 :
upDown = true;
break;
case 83 :
downDown = true;
break;
case 40 :
downDown = true;
break;
}
}
function checkUp(e:KeyboardEvent):void {
switch (e.keyCode) {
case 65 :
leftDown = false;
break;
case 37 :
leftDown = false;
break;
case 68 :
rightDown = false;
break;
case 39 :
rightDown = false;
break;
case 87 :
upDown = false;
break;
case 38 :
upDown = false;
break;
case 83 :
downDown = false;
break;
case 40 :
downDown = false;
break;
}
}
function update(e:Event):void {
if (upDown) {
if (genericHolder.y >= boundariesLevelOne[(room*4) - 4]) {
genericHolder.y = boundariesLevelOne[(room*4) - 4];
Player.y-=power;
}
if (genericHolder.y < boundariesLevelOne[(room*4) - 4]) {
genericHolder.y+=power;
}
Player.Stand.visible = false;
Player.Forwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = false;
Player.Backwards.visible = true;
playerDirection = 2;
}
if (downDown) {
if (genericHolder.y <= boundariesLevelOne[(room*4) - 3]) {
genericHolder.y = boundariesLevelOne[(room*4) - 3];
Player.y+= power;
}
if (genericHolder.y > boundariesLevelOne[(room*4) - 3]) {
genericHolder.y-=power;
}
Player.Stand.visible = false;
Player.Backwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = false;
Player.Forwards.visible = true;
playerDirection = 1;
}
if (leftDown) {
if (genericHolder.x >= boundariesLevelOne[(room*4) - 2]) {
genericHolder.x = boundariesLevelOne[(room*4) - 2];
Player.x -= power;
}
if (genericHolder.x < boundariesLevelOne[(room*4) - 2]) {
genericHolder.x+=power;
}
Player.Stand.visible = false;
Player.Backwards.visible = false;
Player.Forwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = true;
playerDirection = 4;
}
if (rightDown) {
if (genericHolder.x <= boundariesLevelOne[(room*4) - 1]) {
genericHolder.x = boundariesLevelOne[(room*4) - 1];
Player.x += power;
}
if (genericHolder.x > boundariesLevelOne[(room*4) - 1]) {
genericHolder.x-=power;
}
Player.Stand.visible = false;
Player.Backwards.visible = false;
Player.Forwards.visible = false;
Player.Lefts.visible = false;
Player.Rights.visible = true;
playerDirection = 3;
}
if (!upDown && !downDown && !leftDown && !rightDown) {
Player.Backwards.visible = false;
Player.Forwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = false;
Player.Stand.visible = true;
Player.Stand.gotoAndStop(playerDirection);
}
if (Player.Stand) {
Player.Stand.gotoAndStop(playerDirection);
}
}
TL;DR The background moves instead of the player, and movement is controlled by variables.
How can I Hittest against any of the sprites that I've added? At the moment, if I try to Hittest a specific Sprite, it tests agains the entire Sprite holder, not the specific Sprite I want.

Are you using the hittest method?
http://www.actionscriptmoron.com/AS3Tutorials/hittest-hittestobject/
What do you mean it tests again the entire sprite holder just get the reference of the contained object and pass it to the hittest method. it should work.

Related

set visibility trough property not working actionscript 3.0

This is my first post.
if (condition) {
trace("called");
p[1].visible = false;
j[1].visible = false;
}
With the code above "called" was printed in console but the both objects (buttons) still visible. Then when I try to put the set visibility (p[1].visible = false; and j[1].visible = false;) out from condition, it's work well.
I wonder what the problem here and how can I do set visibility with some condition?
[EDIT]
This is my actual source code. The code snippet above just to simple my question.
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.Event;
stop();
var isInit:Boolean;
var val:Array;
var p:Array;
if (!isInit)
{
isInit = initial();
}
function initial():Boolean
{
trace("init");
val = new Array();
val[1] = 0;
val[2] = 0;
val[3] = 0;
val[4] = 0;
val[5] = 0;
val[6] = 0;
pinit();
jinit();
ainit();
binit();
cinit();
dinit();
einit();
return true;
}
function pinit():void
{
p = new Array();
p[1] = p1;
p[2] = p2;
p[3] = p3;
}
// event listener works
p[1].addEventListener(MouseEvent.CLICK, function (event:MouseEvent):void {
p[1].visible = false;
});
p[2].addEventListener(MouseEvent.CLICK, function (event:MouseEvent):void {
p[2].visible = false;
});
if (isInit)
{
trace("set visibility"); // this is printed as well
var i:int;
for (i = 1; i <= 3; i++)
{
setVisibility(i, val[i]);
}
}
function setVisibility(num:int, val:int):void
{
if (val==0)
{
p[num].visible = true;
j[num].visible = true;
}
else if (val==1)
{
trace("one");
p[num].visible = false;
j[num].visible = false;
}
else if (val==2)
{
trace("two");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
}
else if (val==3)
{
trace("three");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
}
else if (val==4)
{
trace("four");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
}
else if (val==5)
{
trace("five");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
d[num].visible = false;
}
}
The ainit, binit, cinit, etc functions are in another layer (on same frame) because the objects are there. The "one", "two", "three", "four", or "five" is printed but the visibility not set correctly.
Just to make your code simpler and easier to read.
With your val array you can populate it like this
val = new Array (0,0,0,0,0,0,0);
Yes this has seven numbers as an array starts at 0. You don't have to reference it but I would populate it just to make it look nicer.
I also changed the if else bit at the end to this:
switch (val){
case 0:
p[num].visible = true;
j[num].visible = true;
break;
case 1:
trace("one");
p[num].visible = false;
j[num].visible = false;
break;
case 2:
trace("two");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
break;
case 3:
trace("three");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
break;
case 4:
trace("four");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
break;
case 5:
trace("five");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
d[num].visible = false;
break;
}
As to your actual problem I don't think you can change a button from an array as it would become a copy of the button not the button itself (Hope that makes sense). I would still have the arrays but instead of the button visibility I'd use Booleans. So that at the end you could put:
btnButton.visible = p1;
Hope that's all Okay and of some use.

game maker - how to navigate a map with 2d array

I have to make a navigable map
the starting point is in the center
there are three worlds + the final stage
pressing up I have to navigate from the base to the first level of the first world and go next level in second and third
pressing down I have do go from the third to the second and from the second to the first level and from the first level of the world to the base
pressing left and right I have to change the world
now:
I already made a lot of menus using different methods, but alway using a 1d array
obj_menu:
create event:
///menu
menu[0] = "new";
menu[1] = "load";
menu[2] = "exit";
space = 55;
mpos = 0;
step event:
///move
if(inputs) {
var move = 0;
move -= max(keyboard_check_pressed(vk_up),0);
move += max(keyboard_check_pressed(vk_down),0);
if(move != 0) {
mpos += move;
if(mpos < 0) {
mpos = array_length_1d(saveload) -1;
}
if(mpos > array_length_1d(saveload) -1) {
mpos = 0;
}
}
//push
if(keyboard_check_pressed(vk_enter)) {
scr_menu();
}
}
scr_menu();
switch(mpos) {
case 0: { scr_new_game(); break; } //new
case 1: { scr_load_game(); break; } //load
case 2: { game_end(); break; } //exit
default: { break; }
}
This time I have to navigate in a 2d array
I did this:
obj_map:
create event:
///navigation setup
if(crash_to_base) { lvl[0,0] = true }
if(base_to_ruins_1) { lvl[1,0] = true }
if(ruins_1_to_ruins_2) { lvl[1,1] = true }
if(ruins_2_to_ruins_3) { lvl[1,2] = true }
if(base_to_city_1) { lvl[2,0] = true }
if(city_1_to_city_2) { lvl[2,1] = true }
if(city_2_to_city_3) { lvl[2,2] = true }
if(base_to_lab_1) { lvl[3,0] = true }
if(lab_1_to_lab_2) { lvl[3,1] = true }
if(lab_2_to_lab_3) { lvl[3,2] = true }
if(base_to_castle) { lvl[4,0] = true }
//posizione del menu
mposh = 0;
mposv = 0;
mpos[mposv,mposh] = 0;
step event:
///map navigation
if(inputs) {
moveh -= max(keyboard_check_pressed(vk_left),0);
moveh += max(keyboard_check_pressed(vk_right),0);
movev -= max(keyboard_check_pressed(vk_up),0);
movev += max(keyboard_check_pressed(vk_down),0);
if(moveh != 0) {
//mposh += move;
}
if(movev != 0) {
//mposv += move;
}
push = keyboard_check_pressed(vk_enter);
if(push) {
scr_map();
}
}
how to translate the first method to de sencond need??
Not quite sure what your having difficulty with, perhaps you could elaborate on what exactly the problem is? On a side note however your 1D menu navigation code can be greatly simplified to:
mpos += keyboard_check_pressed(vk_up) - keyboard_check_pressed(vk_down);
var len = array_length_1d(saveload);
if (mpos < 0) { mpos = len - 1; }
if (mpos > len - 1) { mpos = 0; }
and in terms of a 2d map navigation system, it might not be beneficial to use 2d arrays and instead you could use a ds_map which allows you to store all information on each location in one data structure. For instance
var lvlMap = ds_map_create()
lvlMap[?"base-title"] = "Base"
lvlMap[?"base-travel-right"] = "crash"
lvlMap[?"base-travel-left"] = "fortress"
then when you try to move right/left:
var next_location = lvlMap[?current_location+"-travel-"+direction]
current_location = next_location

How to use keyboard in wpf calculator

Enter button is not working when i try to get the result
public partial class Window1 : Window
{
static MyTextBox DisplayBox;
static MyTextBox PaperBox;
static PaperTrail Paper;
public Window1()
: base()
{
InitializeComponent();
//sub-class our textBox
//DisplayBox = new MyTextBox();
//Grid.SetRow(DisplayBox, 0);
//Grid.SetColumn(DisplayBox, 0);
//Grid.SetColumnSpan(DisplayBox, 9);
//DisplayBox.Height = 30;
//MyGrid.Children.Add(DisplayBox);
//sub-class our paper trail textBox
PaperBox = new MyTextBox();
Grid.SetRow(PaperBox, 1);
Grid.SetColumn(PaperBox, 0);
Grid.SetColumnSpan(PaperBox, 3);
Grid.SetRowSpan(PaperBox, 5);
PaperBox.IsReadOnly = true;
PaperBox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
PaperBox.Margin = new Thickness(3.0,1.0,1.0,1.0);
PaperBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
Paper = new PaperTrail();
MyGrid.Children.Add(PaperBox);
ProcessKey('0');
EraseDisplay = true;
}
private enum Operation
{
None,
Devide,
Multiply,
Subtract,
Add,
Percent,
Sqrt,
OneX,
Negate
}
private Operation LastOper;
private string _display;
private string _last_val;
private string _mem_val;
private bool _erasediplay;
//flag to erase or just add to current display flag
private bool EraseDisplay
{
get
{
return _erasediplay;
}
set
{
_erasediplay = value;
}
}
//Get/Set Memory cell value
private Double Memory
{
get
{
if (_mem_val == string.Empty)
return 0.0;
else
return Convert.ToDouble(_mem_val);
}
set
{
_mem_val = value.ToString();
}
}
//Lats value entered
private string LastValue
{
get
{
if (_last_val == string.Empty)
return "0";
return _last_val;
}
set
{
_last_val = value;
}
}
//The current Calculator display
private string Display
{
get
{
return _display;
}
set
{
_display = value;
}
}
// Sample event handler:
private void OnWindowKeyDown(object sender, System.Windows.Input.TextCompositionEventArgs /*System.Windows.Input.KeyEventArgs*/ e)
{
string s = e.Text;
char c = (s.ToCharArray())[0];
e.Handled = true;
if ((c >= '0' && c <= '9') || c == '.' || c == '\b') // '\b' is backspace
{
ProcessKey(c);
return;
}
switch (c)
{
case '+':
ProcessOperation("BPlus");
break;
case '-':
ProcessOperation("BMinus");
break;
case '*':
ProcessOperation("BMultiply");
break;
case '/':
ProcessOperation("BDevide");
break;
case '%':
ProcessOperation("BPercent");
break;
case '=':
ProcessOperation("BEqual");
break;
}
}
private void DigitBtn_Click(object sender, RoutedEventArgs e)
{
string s = ((Button)sender).Content.ToString();
//char[] ids = ((Button)sender).ID.ToCharArray();
char[] ids = s.ToCharArray();
ProcessKey(ids[0]);
}
private void ProcessKey(char c)
{
if (EraseDisplay)
{
Display = string.Empty;
EraseDisplay = false;
}
AddToDisplay(c);
}
private void ProcessOperation(string s)
{
Double d = 0.0;
switch (s)
{
case "BPM":
LastOper = Operation.Negate;
LastValue = Display;
CalcResults();
LastValue = Display;
EraseDisplay = true;
LastOper = Operation.None;
break;
case "BDevide":
if (EraseDisplay) //stil wait for a digit...
{ //stil wait for a digit...
LastOper = Operation.Devide;
break;
}
CalcResults();
LastOper = Operation.Devide;
LastValue = Display;
EraseDisplay = true;
break;
case "BMultiply":
if (EraseDisplay) //stil wait for a digit...
{ //stil wait for a digit...
LastOper = Operation.Multiply;
break;
}
CalcResults();
LastOper = Operation.Multiply;
LastValue = Display;
EraseDisplay = true;
break;
case "BMinus":
if (EraseDisplay) //stil wait for a digit...
{ //stil wait for a digit...
LastOper = Operation.Subtract;
break;
}
CalcResults();
LastOper = Operation.Subtract;
LastValue = Display;
EraseDisplay = true;
break;
case "BPlus":
if (EraseDisplay)
{ //stil wait for a digit...
LastOper = Operation.Add;
break;
}
CalcResults();
LastOper = Operation.Add;
LastValue = Display;
EraseDisplay = true;
break;
case "BEqual":
if (EraseDisplay) //stil wait for a digit...
break;
CalcResults();
EraseDisplay = true;
LastOper = Operation.None;
LastValue = Display;
//val = Display;
break;
case "BSqrt":
LastOper = Operation.Sqrt;
LastValue = Display;
CalcResults();
LastValue = Display;
EraseDisplay = true;
LastOper = Operation.None;
break;
case "BPercent":
if (EraseDisplay) //stil wait for a digit...
{ //stil wait for a digit...
LastOper = Operation.Percent;
break;
}
CalcResults();
LastOper = Operation.Percent;
LastValue = Display;
EraseDisplay = true;
//LastOper = Operation.None;
break;
case "BOneOver":
LastOper = Operation.OneX;
LastValue = Display;
CalcResults();
LastValue = Display;
EraseDisplay = true;
LastOper = Operation.None;
break;
case "BC": //clear All
LastOper = Operation.None;
Display = LastValue = string.Empty;
Paper.Clear();
UpdateDisplay();
break;
case "BCE": //clear entry
LastOper = Operation.None;
Display = LastValue;
UpdateDisplay();
break;
case "BMemClear":
Memory = 0.0F;
DisplayMemory();
break;
case "BMemSave":
Memory = Convert.ToDouble(Display);
DisplayMemory();
EraseDisplay = true;
break;
case "BMemRecall":
Display = /*val =*/ Memory.ToString();
UpdateDisplay();
//if (LastOper != Operation.None) //using MR is like entring a digit
EraseDisplay = false;
break;
case "BMemPlus":
d = Memory + Convert.ToDouble(Display);
Memory = d;
DisplayMemory();
EraseDisplay = true;
break;
}
}
private void OperBtn_Click(object sender, RoutedEventArgs e)
{
ProcessOperation(((Button)sender).Name.ToString());
}
private double Calc(Operation LastOper)
{
double d = 0.0;
try {
switch (LastOper)
{
case Operation.Devide:
Paper.AddArguments(LastValue + " / " + Display);
d = (Convert.ToDouble(LastValue) / Convert.ToDouble(Display));
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.Add:
Paper.AddArguments(LastValue + " + " + Display);
d = Convert.ToDouble(LastValue) + Convert.ToDouble(Display);
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.Multiply:
Paper.AddArguments(LastValue + " * " + Display);
d = Convert.ToDouble(LastValue) * Convert.ToDouble(Display);
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.Percent:
//Note: this is different (but make more sense) then Windows calculator
Paper.AddArguments(LastValue + " % " + Display);
d = (Convert.ToDouble(LastValue) * Convert.ToDouble(Display)) / 100.0F;
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.Subtract:
Paper.AddArguments(LastValue + " - " + Display);
d = Convert.ToDouble(LastValue) - Convert.ToDouble(Display);
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.Sqrt:
Paper.AddArguments("Sqrt( " + LastValue + " )");
d = Math.Sqrt(Convert.ToDouble(LastValue));
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.OneX:
Paper.AddArguments("1 / " + LastValue);
d = 1.0F / Convert.ToDouble(LastValue);
CheckResult(d);
Paper.AddResult(d.ToString());
break;
case Operation.Negate:
d = Convert.ToDouble(LastValue) * (-1.0F);
break;
}
}
catch {
d = 0;
Window parent = (Window)MyPanel.Parent;
Paper.AddResult("Error");
MessageBox.Show(parent, "Operation cannot be perfomed", parent.Title);
}
return d;
}
private void CheckResult(double d)
{
if (Double.IsNegativeInfinity(d) || Double.IsPositiveInfinity(d) || Double.IsNaN(d))
throw new Exception("Illegal value");
}
private void DisplayMemory()
{
if (_mem_val != String.Empty)
BMemBox.Text = "Memory: " + _mem_val;
else
BMemBox.Text = "Memory: [empty]";
}
private void CalcResults()
{
double d;
if (LastOper == Operation.None)
return;
d = Calc(LastOper);
Display = d.ToString();
UpdateDisplay();
}
private void UpdateDisplay()
{
if (Display == String.Empty)
DisplayBox.Text = "0";
else
DisplayBox.Text = Display;
}
private void AddToDisplay(char c)
{
if (c == '.')
{
if (Display.IndexOf('.', 0) >= 0) //already exists
return;
Display = Display + c;
}
else
{
if (c >= '0' && c <= '9') {
Display = Display + c;
}
else
if (c == '\b') //backspace ?
{
if (Display.Length <= 1)
Display = String.Empty;
else
{
int i = Display.Length;
Display = Display.Remove(i - 1, 1); //remove last char
}
}
}
UpdateDisplay();
}
void OnMenuAbout(object sender, RoutedEventArgs e)
{
Window parent = (Window)MyPanel.Parent;
MessageBox.Show(parent, parent.Title + " - By Jossef Goldberg ", parent.Title,MessageBoxButton.OK, MessageBoxImage.Information);
}
void OnMenuExit(object sender, RoutedEventArgs e)
{
this.Close();
}
void OnMenuStandard(object sender, RoutedEventArgs e)
{
//((MenuItem)ScientificMenu).IsChecked = false;
((MenuItem)StandardMenu).IsChecked = true; //for now always Standard
}
//Not implemenetd
void OnMenuScientific(object sender, RoutedEventArgs e)
{
//((MenuItem)StandardMenu).IsChecked = false;
}
private class PaperTrail
{
string args;
public PaperTrail()
{
}
public void AddArguments(string a)
{
args = a;
}
public void AddResult(string r)
{
PaperBox.Text += args + " = " + r + "\n";
}
public void Clear()
{
PaperBox.Text = string.Empty;
args = string.Empty;
}
}
}
<
my keyboard is not working specially equal to operator so my intention is when i click on enter button from keyboard the calculator should show result>.
private void _KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter || e.Key == Key.Tab)
{
//YourCode
}
}
private void txtEdtDisplayBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
string rest = txtEdtDisplayBox.Text;
string[] seperaateNumbers;
seperaateNumbers = rest.Split('+');
if (seperaateNumbers.Count() > 1)
{
txtEdtDisplayBox.Text = Convert.ToString(Convert.ToInt64(seperaateNumbers[0]) + Convert.ToInt64(seperaateNumbers[1]));
//MessageBox.Show( Convert.ToString(Convert.ToInt64(seperaateNumbers[0]) + Convert.ToInt64(seperaateNumbers[1])));
}
else
{
DXMessageBox.Show("Wrong Operation","Error", MessageBoxButton.OK,MessageBoxImage.Error);
}
seperaateNumbers = rest.Split('-');
if (seperaateNumbers.Count() > 1)
{
txtEdtDisplayBox.Text = Convert.ToString(Convert.ToInt64(seperaateNumbers[0]) + Convert.ToInt64(seperaateNumbers[1]));
}
seperaateNumbers = rest.Split('*');
if (seperaateNumbers.Count() > 1)
{
txtEdtDisplayBox.Text = Convert.ToString(Convert.ToInt64(seperaateNumbers[0]) + Convert.ToInt64(seperaateNumbers[1]));
}
seperaateNumbers = rest.Split('/');
if (seperaateNumbers.Count() > 1)
{
txtEdtDisplayBox.Text = Convert.ToString(Convert.ToInt64(seperaateNumbers[0]) + Convert.ToInt64(seperaateNumbers[1]));
}
}
}

How can I use the array that I have at frame 1 to another frame?

When I'm going to draw the next stage at frame 2 my enemies did nothing.
When the stage1 finished I remove the listeners and at stage 2 I add it back.
So, How can I use the array that I have at frame 1 to another frame?
stop();
var enemy1Array:Array = new Array();
for (var e1:int = numChildren - 1; e1 >= 0; e1--)
{
var childe1:DisplayObject = getChildAt(e1);
if (childe1.name.indexOf("enemy")>-1)
{
enemy1Array.push(MovieClip(childe1));
MovieClip(childe1).hitPoints=enemykoufoueshitpoints;
}
}
stage.addEventListener(Event.ENTER_FRAME,loop);
function loop(event:Event):void
{
for(var e:int=enemy1Array.length-1;e>=0;e--)
{
if (playerrun == true)
{
enemy1Array[e].x -=speedall;
}
if (playerrunback == true)
{
enemy1Array[e].x +=speedall;
}
if (enemy1Array[e].hitTestPoint(player.x+5,player.y-40))
{
trace("i heart the player");
zoihit++;
enemy1Array[e].x +=70;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestPoint(player.x-5,player.y-40))
{
trace("i heart the player");
zoihit++;
enemy1Array[e].x -=70;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestObject(knife)&&attackright == true)
{
attackright = false;
enemy1Array[e].hitPoints -= knifedamage;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestObject(knife)&&attackleft == true)
{
attackleft = false;
enemy1Array[e].hitPoints -= knifedamage;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitPoints <= 0)
{
var thkiamantoui:thkiamanti= new thkiamanti;
diamondArray.push(thkiamantoui);
this.addChild(thkiamantoui);
thkiamantoui.gotoAndStop(2);
thkiamantoui.x = enemy1Array[e].x;
thkiamantoui.y = enemy1Array[e].y-5.05;
enemy1Array[e].parent.removeChild(enemy1Array[e]);
enemy1Array.splice(e,1);
}
if(enemy1Array[e].hitTestObject(dioswall))
{
enemy1Array[e].parent.removeChild(enemy1Array[e]);
enemy1Array.splice(e,1);
}
}
if(stage2.hitTestObject(playerhit))
{
stage.removeEventListener(Event.ENTER_FRAME,loop);
gotoAndPlay(2);
}
}

ActionScript 3 Array Return Runtime Error

All right so I have the following code and all it does is put 3 solid-colour squares on the screen and one rainbow-coloured one in the bottom-right corner. When the user presses on any of the solid-coloured squares, that spot get filled with the rainbow-coloured one and in the original location of the rainbow goes that square that was clicked. The code works almost perfectly except for one thing. When the user tries to click on a square that is UNDER the rainbow square, it returns a run-time error.
My Code:
i
mport flash.display.DisplayObject;
import flash.ui.Mouse;
var t1:DisplayObject = new mc_1;
var t2:DisplayObject = new mc_2;
var t3:DisplayObject = new mc_3;
var t4:DisplayObject = new mc_4;
var tile:Array = [[t1,t2],[t3,t4]];
var r:int;
var c:int;
var a:int = 50;
var b:int = 50;
var aa:int = 1;
var bb:int = 1;
function reDraw() {
a = 50;
b = 50;
for (r=0;r<2;r++) {
for (c=0;c<2;c++) {
tile[r][c].x = a;
tile[r][c].y = b;
trace(tile[r][c]);
stage.addChild(tile[r][c]);
tile[r][c].addEventListener(MouseEvent.CLICK, go);
a += 100;
}
a = 50;
b += 100;
}
}
reDraw();
function go(e:MouseEvent):void {
trace(e.target);
//Right:
if (e.target == tile[aa][bb+1]) {
tile[aa][bb] = e.target;
bb += 1;
tile[aa][bb] = t4;
reDraw();
trace("Right");
}
//Left:
else if (e.target == tile[aa][bb-1]) {
tile[aa][bb] = e.target;
bb -= 1;
tile[aa][bb] = t4;
reDraw();
trace("Left");
}
//Up:
else if (e.target == tile[aa-1][bb]) {
tile[aa][bb] = e.target;
aa -= 1;
tile[aa][bb] = t4;
reDraw();
trace("Up");
}
//Down:
else if (e.target == tile[aa+1][bb]) {
tile[aa][bb] = e.target;
aa += 1;
tile[aa][bb] = t4;
reDraw();
trace("Down");
}
else trace("FAILED!");
trace(aa +" " + bb);
}
The error:
TypeError: Error #1010: A term is undefined and has no properties. at
win_fla::MainTimeline/go()
If you take a look at your code you have this:
//Down:
else if (e.target == tile[aa+1][bb]) {
tile[aa][bb] = e.target;
aa += 1;
tile[aa][bb] = t4;
reDraw();
trace("Down");
}
now you can see here that its looking for tile[aa+1] however aa = 1 in the beginning so aa+1 = 2 and tile[2] doesn't exist or is undefined. You'll need to change your logic there to something like:
var tileFound:Boolean = false;
for(var i:int = 0; i < 2; i++){
for(var j:int = 0; j < 2; j++){
if(tile[i][j] == e.target){
tileFound = true;
tile[aa][bb] = e.target;
tile[i][j] = t4;
if(i > aa) trace ("Right");
else if(i < aa) trace ("Left");
if(j > bb) trace ("Bottom");
else if(j < bb) trace ("Top");
aa = i;
bb = j;
reDraw();
tileFound = true;
break;
}
}
if(tileFound) break;
}

Resources