So when writing a game on Khan Academy When I try to remove a bullet from the array I run into the error "Object does not support method splice" I have been checking my code for hours and have not found out why it does not work. Ideas?
EDIT: The code used to remove a bullet is bullets[i].splice(i,1); and that is what errors out my code.
MVCE:
var bullets = [];
var bullet= function(x,y,blah)
{
//code that is not important here
};
bullets.push(bullet(0,0,30));
for(var I = 0; I < bullets.length; I++){
if(bulletRemove){
bullets[I].splice(i,1)
}
}
You have a variable named bullets:
var bullets = [];
(Side note: Why is there a random curly bracket right before this line?)
This bullets variable is an array. It holds instances of the Bullet class:
bullets.push(new Bullet(x, y, 10, player.x+bSize/2, player.y+bSize/2));
You can use the array to access a Bullet at a particular index, and then you can call functions of the Bullet class on that instance:
bullets[i].move();
You can also call the splice() function on the array itself:
bullets.splice(i,1);
However, you can't call the splice() function on a particular Bullet instance!
bullets[i].splice(i,1);
This line is taking an instance of Bullet from the i index of the bullets array, and then trying to call the splice() function from the Bullet class. But the Bullet class doesn't have a splice() function! This is what's causing the error.
Instead, you probably meant to call it on the array itself:
bullets.splice(i,1);
In the future, please please please try to narrow your problem down before posting a question. Try to post an MCVE instead of your entire project. You could have put together an example program that used just a few lines to create a hard-coded array and used that to demonstrate your problem. Chances are you would have found the problem yourself in the process of creating the MCVE!
I am having this problem for a few days now and I can not find a way around it.
I'm trying to store an array of cv::Mats and the way I was doing this so far was by having a a
std::vector<cv::Mat> store_frames(32); and every time I was just copying the new frames to their location with frame.copyTo(store_frames[i]).
This works fine if it's inside main but if I want to pass it to a function and do the same thing it's not good anymore.
I did not have the same problem with simple mats. I was just declaring them globally inside a header
cv::Mat frame;
And if I had a function:
void function( cv::Mat &f1 ) {
..
f1.at<float>(10,10) += 100;
..
}
Then call the function function(frame);
All the changes inside the function would be saved. So let's say I will have something like
...
for(int i=0; i<5; i++) {
function(frame);
std::cout<<frame.at<float>(10,10)<<' ';
}
The output (assuming the initial value of frame.at(10,10) would be
100 200 300 400 500
The problem now is that I can not do the same thing with std::vector store_frames . First of all if I try to declare it in the header the same way std::vector<cv::Mat> store_frames(32); I get an error on number 32 saying Error: expected a type specifier. And if I just say std::vector<cv::Mat> store_frames; then it won't know it's size. I saw somewhere people calling store_frames.resize I've tried to do that, but I get an error saying this declaration has no storage class or type specifier. I've looked up the internet for solutions, but didn't find any. I know the question itself might sound stupid, but I'm new to this and I don't know much.
So my questions is how do I declare this vector so that it would be possible to pass it to a function, modify it inside and the modifications would be saved?
Thank you!
Try
declared it std::vector<cv::Mat> store_frame; in the header and store_frame.resize(32); in the source code.
use std::vector<cv::Mat> store_frames(32); in the source code.
ps: I just added this as the comment. :)
I have a flash game that I'm building where I have an array keeping track of a bunch of power ups on the screen. When the player goes an grabs one it needs to disappear from the screen (which is easy) but it also needs to be removed from the array so that collision detection loops don't become really cumbersome. I tried using splice, but I keep get null reference errors, here's the relevant code.
public function collect():void {
try {
Main.powerUps.splice(index, 1);
stage.removeChild(this);
}catch (e:Error) {
trace("Error in splice");
}
}
}
Then when I create my PowerUp object I pass it a parameter that gets assigned to index which is the length of the array of Power Ups at the time. Can anyone see what's wrong with this code or (preferably) provide a more elegant solution? Thanks in advance for any help.
Elegant solution: use indexOf() and splice() together!
var index:int = Main.powerUps.indexOf( powerup );
Main.powerUps.splice(index, 1);
Where powerup is a reference to the object stored within the array Main.powerUps.
I also created a little class a while back that may be useful to you:
https://github.com/MartyWallace/Lotus/blob/master/Lotus/lotus/utils/Set.as
It has a .remove() method so you can just do:
powerUps.remove(powerup);
i'm having some problem in updating my array which is global by the way.
here is my code:
Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];
thats my global array which i can access data from it from anywhere in the application.
the problem comes when i want to update the array like:
for(var q=0; q<Ti.App.dinercolor.length; q++){Ti.App.dinercolor[q] = '#dccdc0';}
so, the array i was expecting after the operation thats done is something like this:
Ti.App.dinercolor=["#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0"];
but somehow i'm getting the same array with out updating,
Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];
please help me out, i have no idea what i'm doing wrong here,
Thank you,,
Your code is correct, but you shouldn't extend the Ti object as unexpected things like this will happen. Create your own object and it will work.
myObj = {};
myObj.dinercolor = [];
And so on.
It is recommended you keep your app in a single context so you will be able to access the object from anywhere. Check out the forging titanium video series for some best practices.
I agree with Jeff, however if you want the above approach to work you will need to update the whole array, you cannot just update elements.
So read the array out into a new variable, update the specific elements and then set the property again
In App.js:
Ti.App.my_variable = 0;
In some_other_page.js:
Ti.App.my_variable = 101;
In yet_another_page.js:
alert( Ti.App.my_variable );
This will alert 101 !!
I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC
Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.
When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?