gather total score of quiz questions with new .as class (2 errors currently) - actionscript

I currently created a new actionScript class, called Quiz.as
// package
// {
// public class Quiz
// {
public var knowledgePoints:int = 10;
public var allQuestions:Array = new Array;
public var questionCorrect:Boolean = false;
public function getTotalScore():int
{
// var totalScore:int = 0;
var totalScore = 0;
for (var i = 0; i < allQuestions.length; i++)
{
totalScore += allQuestions[i].getCalculatedScore();
if (currentModule.allQuestions[i].parent.questionCorrect)
{
knowledgePoints++;
}
else
{
knowledgePoints--;
}
}
debugLog("Total score: " + totalScore);
debugLog(knowledgePoints);
return totalScore;
}
// }
//}
This solution derived from my original question at: keeping track of a series of simple multiple choice web form answers
But now with the above code I am getting errors in flash console. My latest attempt to fix these errors are with the commented out regions above.
Error 1. Attribute used outside of class.
So I comment out first couple lines, but error continues to point to whatever line is exposed first.
Error 2. 'Int' could not be loaded.
Thanks for any advice,

Your issue is likely the result of using the above code on the timeline in AnimateCC/FlashPro.
Class files (and their corresponding attributes like public/private) need to be in their own actionscript (.as) file.
To create a class file in AnimateCC, go to file -> new and choose ActionScript 3.0 Class. Give it the class name of Quiz (so it matches what you've declared in your code)
Replace the default code with the class file code from your question (you'll need to restore those commented out lines too)
Save the file in the same directory as your .fla
To use your new class file in the timeline, you'd do something like this:
var quiz:Quiz = new Quiz(); //make an instance of your custom class
quiz.allQuestions.push(whateverAQuestionIs); //add a question to your array
trace(quiz.getTotalScore()); //trace out the total score
If you want to use that code in the timeline and forgo using a class file, you'll just need to remove the 4 public keywords (in addition to the lines you've already commented out)

Related

Creating and changing existent Methods for Dart classes

I'm trying to program with Dart on Visual Studio Code and need to understand the Syntax of Dart a bit more to be able to code what is needed.
These are the classes I have coded:
Book (defines book title, isbn, price and availability)
class Book {
// properties
late String _title;
late int _isbn;
late double _price;
bool _isAvailable;
....
}
BookCatalog (imports prior class and defines current list of books + methods to add new books)
class BookCatalog{
// no arrays, everything is a list
List<Book> _books = [];
...
}
Main (imports prior classes and executes wanted input)
What I want to change:
improve addBook Method within BookCatalog Class, to not add a new Book, if it has the same ISBN as a already existing one, which it currently does. (Potentially give out message stating 'false' if user tries to add Book with same ISBN)
// adds a book to the store
void addBook(book) {
_books.add(book);
}
improve printBookCatalog Method within BookCatalog Class, to only print available Books and not all Books added to the Store.
// Print books contained in the catalog
void printBookCatalog() {
print("List of Books:");
for (var book in _books) {
print(book);
}
}
improve getBook Method within BookCatalog Class, to actually get the book by its ID, as it's not working right now.
// Get a book by id
Book getBook(int place) => _books[place];
improve calculateAvgPrice Method within BookCatalog Class, to calculate the average price of Books, it's not working correctly as of now
// NEW, calculates average price of books
Book calculateAvgPrice(double price) {
for(var book in _books) {
sum += book.Price;
}
return sum / _books.length;
}
create a new Method within BookCatalog Class to delete Books from the Catalog
answer could be, but I'm not sure :
// deletes book from the catalog
void deleteBook(book) {
_books.remove(book);
}
Any help, explanations, examples or external sources to read up on Dart would be appreciated!
(Especially examples for my specific cases)
I suggest going through Dart Language Tour first.
(Side note: ISBN has leading zero and hyphen, so it's better to use a String for it instead of int)
In your BookCatalog class, you could use a Map instead of a List:
class BookCatalog {
Map<String, Book> _books = {}; // Map from ISBN to Book
// ...
}
Here I'm assuming that both classes are in the same file, otherwise accessing private members wouldn't work, so you'd have to have public getters available.
/// Adds a book to the store only if the ISBN does NOT exist.
bool addBook(Book book) {
if (_books.containsKey(book._isbn)) {
return false;
}
_books[book._isbn] = book;
return true;
}
Missing the if conditional.
void printBookCatalog() {
print("List of Books:");
for (final book in _books.values) {
if (book._isAvailable) {
print(book);
}
}
}
Assuming that by id you mean, its ISBN. It returns a nullable Book? because the isbn might not exist in the catalog.
// Get a book by id
Book? getBook(String isbn) => _books[isbn];
The function has a return type of double not a Book as it's the average price. You also don't need to provide any argument to this function. And you need to define the sum variable first before using it!
double calculateAvgPrice() {
var sum = 0.0;
for (final book in _books.values) {
sum += book._price;
}
return sum / _books.length;
}
Assuming that you want to remove books by their ISBN:
bool removeBook(String isbn) {
return _books.remove(isbn) != null;
}

frida modify static param as ArrayList

public class OBDSportsModelManager {
public static ArrayList<DataArray> mDiagnosisCommand;
public boolean getData() {
mDiagnosisCommand = new ArrayList<>();
for (String dataArray : this.commandIDs) {
mDiagnosisCommand.add(new DataArray(dataArray));
}
return true;
}
}
I want to add some more item to the 'mDiagnosisCommand',
by using this code:
sports.getData.implementation = function(){
Log.v("hook-sports", "try to add obd commands!");
var ret = this.getData();
var DataArray = Java.use("com.obd2.comm.DataArray");
var items = DataArray.$new("0x00,0x00,0x00,0x00,0x00,0x42");
this.mDiagnosisCommand.add(items); // not working!!!
Log.v("hook-sports", "hook done!");
return ret;
}
but doesn't works well.
I googled frida ArrayList add items without any help.
You have two problems:
You are using this.mDiagnosisCommand but the field is a static field, therefore it belongs to the class OBDSportsModelManager and not to the class instance this.
By calling this.mDiagnosisCommand you only get the Frida object representing this field, not the field value itself. If you want the ArrayList referenced by a field you have to add .value.
Considering both problems the following lines should work (after correcting the class name):
// correct the class name in the next line
var cls = Java.use("<full.name.to>.OBDSportsModelManager");
cls.mDiagnosisCommand.value.add(items);

How to use a variable from a different class

I have a rather simple question. How can I use variables from different classes in dart?
class ContainsVariable {
var variable = 1;
}
class DoesNotContainVariable {
var useVariable = variable + 1; // This gives me an error saying:
// Undefined name 'variable'
}
Having their own scope is a very fundamental feature of classes in Object Oriented Programming, corresponding to OOP principles.
Also note that from your code, it seems that you have not properly understood the idea of instantiation in Object Oriented Programming, since you are trying to set an instance variable without instantiating the class. I highly suggest to look into this topic to gain more understanding.
That being said, there are most definitely many ways to achieve what you want. Since your code sample is very general, I'm not exactly sure what you are trying to do, so I'll provide 2 examples, which might be useful:
Option 1 - static member variable
You can make a static (class level) member, which will be the same for all objects.
class ContainsVariable {
static var variable = 1;
}
class DoesNotContainVariable {
var useVariable = ContainsVariable.variable + 1; // here, you are using a
// static (class) variable,
// not an instance variable.
// That is why you are using
// the class name.
}
Option 2 - instantiation
You can instantiate the class - by creating an object of that class - and access the member of that object. Notice that there is no static statement here.
class ContainsVariable {
var variable = 1;
}
class DoesNotContainVariable {
var instanceOfContainsVariable;
var useVariable;
DoesNotContainVariable(){ // this is a constructor function
var instanceOfContainsVariable = new ContainsVariable();
useVariable = instanceOfContainsVariable.variable + 1;
}
}

Microsoft.Xna.Framework.Content.ContentLoadException in MonoGame when calling string from a stored xnb file path

(I am using VS2012 Ultimate to make Windows Phone 8 Game with MonoGame)
So I have a class called ImagePathStorer.
The purpose of the class is to store the string path from my xnb background file so, instead of writing below code in my LoadContent method (in DrawableGameComponent):
_bg = Game.Content.Load<Texture2D>(#"Resources/Images/Backgrounds/Loading");
I can write this instead to make it cleaner, and easier to maintain:
_bg = Game.Content.Load<Texture2D>(images.getBackgroundsPath(BackgroundName.Loading));
Note :
_bg is a Texture2D object.
BackgroundName is an enum to store the name of Backgrounds in my project.
And this is the ImagePathStorer class :
public class ImagePathStorer
{
private List<string> backgroundsPath;
public ImagePathStorer()
{
InitBackgroundsPath();
}
private void InitBackgroundsPath()
{
backgroundsPath = new List<string>();
backgroundsPath.Insert((int)BackgroundName.Battle, #"Resources/Images/Backgrounds/Battle");
backgroundsPath.Insert((int)BackgroundName.Loading, #"Resources/Images/Backgrounds/Loading");
backgroundsPath.Insert((int)BackgroundName.Option, #"Resources/Images/Backgrounds/Option");
backgroundsPath.Insert((int)BackgroundName.Start, #"Resources/Images/Backgrounds/Start");
backgroundsPath.Insert((int)BackgroundName.Tutorial, #"Resources/Images/Backgrounds/Tutorial");
}
}
But when I compiled it, there is an error message from :
_bg = Game.Content.Load<Texture2D>(images.getBackgroundsPath(BackgroundName.Loading));
....which says : "ContentLoadException was unhandled by user code" "An exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occured in MonoGame.Framework.DLL but was not handled in user code"
I've tried this one instead :
_bg = Game.Content.Load<Texture2D>("#"+images.getBackgroundsPath(BackgroundName.Loading));
but the results are no different. Any idea? Big thanks.

Orchard Taxonomies Shape Alternates by displayType

I am using the shape tracer in order to use an alternate view of one of my taxonomie fields called location. However unlike other shapes the alternates do not give the option for different display types in this case summary or details. So by changing 1 you change the other. I need to be able to do them independently.
I have a view created see below Fields.Contrib.TaxonomyField-Location.cshtml but as i say this is rendered the same if the display type is details or summary.
How to i overcome this please.
Thanks Jon
I had the same problem not long ago. You can provide your own alternates by implementing a ShapeDisplayEvents class.
Here is an implementation that gives you alternates based on the content type, display type, or both:
public class PartContentTypeAlternateFactory : ShapeDisplayEvents {
public override void Displaying(ShapeDisplayingContext context) {
context.ShapeMetadata.OnDisplaying(displayedContext => {
var shapeType = displayedContext.ShapeMetadata.Type;
var contentItem = displayedContext.Shape.ContentItem;
var displayType = displayedContext.ShapeMetadata.DisplayType;
var contentType = contentItem.ContentType;
displayedContext.ShapeMetadata.Alternates.Add(
String.Format("{0}__{1}", shapeType, displayType));
displayedContext.ShapeMetadata.Alternates.Add(
String.Format("{0}__{1}__{2}", shapeType, (string)contentType, displayType));
});
}
}
You will end up with some extra alternates appearing in the shape tracer, like this:
And you can now use an alternate named Fields.Contrib.TaxonomyField-Location-Summary or Fields.Contrib.TaxonomyField-Location-Detail etc. You can extend this class to add whatever alternates you want.
Edit
I didn't realise you couldn't easily get to the field name, so try something like this - it does something similar to what the UrlAlternatesFactory does, i.e. it loops over the existing alternates and adds the displayType to them. This should give you an alternate that contains both the field name and the display type.
public class PartContentTypeAlternateFactory : ShapeDisplayEvents {
public override void Displaying(ShapeDisplayingContext context) {
context.ShapeMetadata.OnDisplaying(displayedContext => {
var alternates = displayedContext.ShapeMetadata.Alternates.Select(a => a + "__" + displayedContext.ShapeMetadata.DisplayType);
displayedContext.ShapeMetadata.Alternates = displayedContext.ShapeMetadata.Alternates.Union(alternates).ToList();
});
}
}

Resources