I'm making my very first text adventure game but keep getting an error thrown at me. I honestly have no clue what's going on or how to even start fixing the issue. I'll post my code and the error message I'm receiving exactly.
PFont computer; //creates a variable for the font to be used
int level = 0; //starting level
int branch = 0; //starting level
String[][] textOptions = new String[][]{
{"Welcome to our text adventure. You are in a room.\n" //new line
+ "It looks like no one has been in here for years.\n"
+ "Would you like to look around? Y/N",""},
{"You decide to look around the room.\n"
+ "You stand by your initial observation.\n"
+ "The room is covered in a thick layer of dust with cobwebs reaching out in every corner.\n"
+ "There are a few shelves, lined with decaying boxes and files. The terminal you are looking for is nowhere to be seen. \n"
+ "You are now in a hallway. There are two rooms to explore. L/R?",
"You decide to leave this room. You are now in a hallway.\n"
+ "There are two rooms to explore. L/R?"},
{"You decide to check the room to the right.",
"You decide to check the room to the left."},
};
void setup()
{
size(1350,700);
computer = createFont("computer.ttf",25);
}
void draw()
{
background(0);
textSize(20);
textFont(computer);
text(textOptions[level][branch],10,25);
fill(0,239,31);
}
void keyPressed()
{
if(key == 'y')
{
println("Player is looking around.");
if(level < 2)
{
level = level+ 1;
branch = 0;
}
}
else if(key == 'n')
{
println("Player decided to leave this room.");
if(level < 2)
{
level = level+ 1;
branch = 1;
}
}
{
if(key == 'r')
{
println("Player has chosen the room to the right.");
if(level < 3)
{
level = level+ 1;
branch = 2;
}
}
else if(key == 'l')
{
println("Player has chosen the room to the left.");
if(level < 3)
{
level = level+ 1;
branch = 3;
}
}
}
}
I keep getting the error code:
java.lang.ArrayIndexOutOfBoundsException: 3
at sketch_171010c.draw(sketch_171010c.java:50)
at processing.core.PApplet.handleDraw(PApplet.java:2437)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
and it appears to be highlighting the portion of draw that says textOptions[level][branch] but I still don't understand what this means or how to fix this issue whatsoever. Any help or input on how to literally do anything would greatly be appreciated. Thank you!
You should get into the habit of debugging your code so you understand exactly what it's doing.
The exception is caused by trying to access an index that an array doesn't have. Here's a simpler example:
String[] array = {"test"};
String x = array[100];
This code would throw an ArrayIndexOutOfBoundsException because the array only has a single element, but we're trying to access index 100. That's what's happening in your code.
Now, why that's happening is going to require debugging your code. Step through it with a piece of paper and a pencil to track the value of the variables, or use the debugger that comes with the Processing IDE.
Related
- mark[1,0]=1; top=1; stack[top].row=1; stack[top].col=0;
stack[top].dir=1; while (top>0) {
loc = pop();
cr=loc.row; cc=loc.col; cd=loc.dir;
while (cd <= 4) {
nr=cr+move[cd].r; nc=cc+move[cd].c;
if (nr==grow && nc==gcol) {
for(i=0; i<=top; i++) printLoc(stack[i]);
print(cr, cc); print(grow, gcol);
return;
}
if (maze[nr][nc]==0 && mark[nr][nc]==0) {
mark[nr][nc] = 1;
loc.row=cr; loc.col=cc; loc.dir=cd+1;
push(loc);
cr=nr; cc=nc; cd=1;
}
else cd=cd+1;
} } print(“no path in maze”);
I've been looking at this algorithm for about an hour but still can't understand it.
I understand that col/row/direction = column, row, direction, and mark means a 2-dimensional array. But I can't get how to implement this algorithm and turn it into code since I can't understand how it works.
Could someone explain this algorithm, please?
More info: 1 = 0,1(move right), 2 = 1,0(move down), 3 = 0, -1(move left), 4 = -1,0(move up). To store the path use stack. If you use stack choose depth-first search, if you use queue choose breadth-first search. 1 is the blocked path while 0 is the path you can walk on.
Last year was my first successful year in teaching my students to create catch and avoid games in Flash with AS3. This year is getting better. Each year I come here for help at least once.
I'm trying to add shooting into the possibilities for their second game project. I can make the shot happen from the ship, gun, whatever, and make it move, and get rid of it when it is off screen, but have not figured out a clean way to have both the shot and the target go away (removeChild and array.splice) upon collision.
The code I have sort of works, but I keep getting the error,
TypeError: Error #1010: A term is undefined and has no properties. at
DropShootV02_fla::MainTimeline/checkShots()
.
Normally I know that this is because of a mismatch between objects and index numbers, but this is related to the call to a second array in removing boxes and bullets.
For simplicity I'll just include the shooting code. Similar organization creates and drops the boxes.
Any help is appreciated. BTW we are not using external script in an AS file.
var shotSpeed = 18;
var shots:Array = new Array();
import flash.events.MouseEvent;
import flash.events.Event;
stage.addEventListener(MouseEvent.CLICK, fireLaser);
function fireLaser(e:MouseEvent):void
{
if (gameOn==true)
{
var shot:Shot=new Shot();
addChild(shot);
shots.push(shot);
shot.gotoAndStop(1);
shot.x = user.x;
shot.y = user.y;
trace(shots.length);
}
}
addEventListener(Event.ENTER_FRAME, moveShot);
function moveShot(e:Event):void
{
for (var i:int=shots.length-1; i>=0; i--)
{
shots[i].y -= shotSpeed;
if (shots[i].y < -25)
{
removeChild(shots[i]);
shots.splice(i,1);
}
}
}
addEventListener(Event.ENTER_FRAME, checkShots);
function checkShots(e:Event):void
{
for (var i:int=shots.length-1; i>=0; i--)
{
for (var k:int=boxes.length-1; k>=0; k--)
{
if (shots[i].hitTestObject(boxes[k]))
{
if (boxes[i].type == "good")
{
score--;
scoreDisplay.text = "Score:" + score;
}
if (boxes[i].type == "bad")
{
score++;
}
removeChild(boxes[k]);
boxes.splice(k,1);
//if I leave this part out I get no errors,
//but but the bullet goes on to strike again
removeChild(shots[i]);
shots.splice(i,1);
}
}
}
}
Thanks kaarto:
I had tried that previously and kept getting the same error. I used that elsewhere in this game code. Turns out I needed to moderate how often the player was shooting. I changed from shooting with the mouse to using space instead and now the problem is gone. Break is definitely a good one here.
Merge move shot and checkShots in one ENTER_FRAME handler.
I was working on writing a screenshot thing, and found this excellent topic for Mac: How can I get screenshot from all displays on MAC?
I was wondering if anyone has the equivalent for x11 library? To get all the monitors and then screenshot them all?
I had found this topic: https://stackoverflow.com/a/5293559/1828637
But the code linked from there is not as easy to follow for a novice like me.
Will RootWindow(3) get the area of all the monitors combined? Then I can go through and get the monitors dimensions then XGetImage those sections on the return of RootWindow?
I had come across this topic: How do take a screenshot correctly with xlib? But I'm not sure if it has multi-monitor support. I do this in ctypes so I cant test that code easily without going through the grueling task of writing it first. So I was wondering if this is correct or how would I modify it to handle multi mon please?
Edit
The poster there shared his code, it is seen here: https://github.com/Lalaland/ScreenCap/blob/master/src/screenCapturerImpl.cpp#L96 but it's complicated and I don't understand it. It uses functions like XFixesGetCursorImage which I can't find in the documentation, and I don't see how the multi monitors work there. Author of that topic warned he doesn't remember the code and it may not work with modern Linux.
This is not a perfect answer to the question, but the following code could be modified to get a very fast version of your desired end result:
https://github.com/Clodo76/vr-desktop-mirror/blob/master/DesktopCapture/main.cpp
The DesktopCapturePlugin_Initialize method converts all the displays into objects:
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API DesktopCapturePlugin_Initialize()
{
DesksClean();
g_needReinit = 0;
IDXGIFactory1* factory;
CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
IDXGIAdapter1* adapter;
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
{
IDXGIOutput* output;
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); j++)
{
DXGI_OUTPUT_DESC outputDesc;
output->GetDesc(&outputDesc);
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(outputDesc.Monitor, &monitorInfo);
// Maybe in future add a function to identify the primary monitor.
//if (monitorInfo.dwFlags == MONITORINFOF_PRIMARY)
{
int iDesk = DeskAdd();
g_desks[iDesk].g_width = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left;
g_desks[iDesk].g_height = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top;
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
IDXGIOutput1* output1;
output1 = reinterpret_cast<IDXGIOutput1*>(output);
output1->DuplicateOutput(device, &g_desks[iDesk].g_deskDupl);
}
output->Release();
}
adapter->Release();
}
factory->Release();
}
Then the OnRenderEvent method copies a frame from the display into a texture (provided by unity in this case):
void UNITY_INTERFACE_API OnRenderEvent(int eventId)
{
for (int iDesk = 0; iDesk < g_nDesks; iDesk++)
{
if (g_desks[iDesk].g_deskDupl == nullptr || g_desks[iDesk].g_texture == nullptr)
{
g_needReinit++;
return;
}
IDXGIResource* resource = nullptr;
const UINT timeout = 0; // ms
HRESULT resultAcquire = g_desks[iDesk].g_deskDupl->AcquireNextFrame(timeout, &g_desks[iDesk].g_frameInfo, &resource);
if (resultAcquire != S_OK)
{
g_needReinit++;
return;
}
g_desks[iDesk].g_isPointerVisible = (g_desks[iDesk].g_frameInfo.PointerPosition.Visible == TRUE);
g_desks[iDesk].g_pointerX = g_desks[iDesk].g_frameInfo.PointerPosition.Position.x;
g_desks[iDesk].g_pointerY = g_desks[iDesk].g_frameInfo.PointerPosition.Position.y;
ID3D11Texture2D* texture;
HRESULT resultQuery = resource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&texture));
resource->Release();
if (resultQuery != S_OK)
{
g_needReinit++;
return;
}
ID3D11DeviceContext* context;
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
device->GetImmediateContext(&context);
context->CopyResource(g_desks[iDesk].g_texture, texture);
g_desks[iDesk].g_deskDupl->ReleaseFrame();
}
g_needReinit = 0;
}
I'm needing to make my character land on a ledge and stay there, but it only keeps going straight through it. Would I create an array for all my different ledges and test whenever my character hits them? Any help would be appreciated.
Thanks.
Collision detection for floors and stuff is actually alot diffrent from hitTesting in the idea that needs to consistently see that the objects are touching. Try something like this!
//loop through all the platform objects to generate the level
var level:Array = new Array();
for (var i=0; i<numChildren; i++)
{
if (getChildAt(i) is platform)
{
level.push(getChildAt(i).getRect(this));
}
}
for (i=0; i<level.length; i++)
{
if (player.getRect(this).intersects(level[i]))
{
if (speedX > 0) ////moving right collision and stuffs
{
player.x = level[i].left-player.width/2;
}
if (speedX < 0) ////moving left collision and stuffs
{
player.x = level[i].right+player.width/2;
}
speedX = 0 //kills the speed
}
}
speedX is the speed at which the characters move horizontally, and "platform" is the name of the variable that you're using as the cliff. Also, "player" can be substituted by whatever you are calling your object that's going onto the ledge. That's how I did it in one of my computer classes anyways :) Hope that helps!
I have a couple of if statements within a timer function, checking if the enemy's hp is 0. The enemies are added into an array. When one enemy hp hits 0, they are taken from the array and removed. When the array length is 0, the game goes to the next level. For some reason, when the first enemy's hp reaches 0, then it automatically goes to the next level without even checking the other enemy's hp. It worked on the first level, where I only had 1 enemy, but the second level, I have two enemies. I would have to defeat the second enemy, then the first in order for it to go to the next level. I think it may have something to do with the first enemy being the first in the array. Any ideas on how I can change the code to make it disregard the order in which enemy is defeated first?
public var enemy:Enemy = new Enemy();
public var enemy2:Enemy = new Enemy();
public var enemyArray:Array = [];
Level Timer(Timer that sets up the level, runs once):
if (level.levelNumber == 1)
{
enemyArray.push(enemy);
addChild(player);
addChild(enemy)
enemy.enemyMoveTimer.start();
enemy.enemyAttackTimer.start();
onGameTimer.start();
}
if (level.levelNumber == 2)
{
enemyArray.push(enemy, enemy2);
addChild(player);
addChild(enemy)
addChild(enemy2)
enemy.enemyMoveTimer.start();
enemy.enemyAttackTimer.start();
enemy2.enemyMoveTimer.start();
enemy2.enemyAttackTimer.start();
onGameTimer.start();
}
Game timer(timer that checks the enemy lives, keeps running):
if (enemyArray.length == 0)
{
trace("NEXT LEVEL");
removeChild(player);//remove player from the current level
onLevelTimer.removeEventListener(TimerEvent.TIMER, timerLevel);
levelNumber += 1;
onLevelTimer.addEventListener(TimerEvent.TIMER, timerLevel);
onGameTimer.stop();
}
if (enemy.enemyHP <= 0)
{
enemy.enemyHP = 0;
enemy.enemyMoveTimer.stop();
enemy.enemyAttackTimer.stop();
trace("DEAD");
enemyArray.splice(0,1);
try
{
removeChild(enemy)
}
catch (e:ArgumentError){
}
}
if (enemy2.enemyHP <= 0)
{
enemy2.enemyHP = 0;
enemy2.enemyMoveTimer.stop();
enemy2.enemyAttackTimer.stop();
trace("DEAD");
enemyArray.splice(1,1);
try
{
removeChild(enemy2)
}
catch (e:ArgumentError) {
}
}
There's more code there that could be the cause of the issue, so I'm just going to assume that it looks something like what I've written below. It also dispenses with multiple if statements, in favor of simply iterating over your enemyArray, checking each as you wanted to (behold the power of loops!).
var enemyMoveTimer:Timer = new Timer(1000, 0);
enemyMoveTimer.addEventListener(TimerEvent.TIMER, enemyTimerListener);
enemyMoveTimer.start();
var enemyArray:Array = [new Enemy("enemy1"), new Enemy("enemy2")];
function enemyTimerListener(e:Event):void {
for (var i:String in enemyArray) {
var enemy:Enemy = enemyArray[int(i)];
if (enemy.enemyHP <= 0) {
enemy.enemyMoveTimer.stop();
enemy.enemyAttackTimer.stop();
trace("DEAD");
enemyArray.splice(int(i),1);
if (enemy.parent != null) {
removeChild(enemy);
}
}
}
}
What isn't shown is how you're detecting and advancing to "the next level". How are these enemies being added to the array? Since it seems you've rolled your own Enemy class, what's that class look like? Why are you using try ... catch?
In any case, if the above works, fun; otherwise, we'll need more code to know what's going on.