Newb question regarding functions and Scope

Lihinel

Veteran
Veteran
Joined
Nov 9, 2013
Messages
261
Reaction score
321
First Language
German
Primarily Uses
Okay, it just hit me, it should have been this.drawName(); so it was a Syntax error on my part, problem is no tutorial I read said how to use a method/function, only how to define it.


-------------------------
Just started out with JS and am a bit uncertain why the following code works, but my attempt at changing and modularising it lead to some problems. First the original:
Scene_Title.prototype.drawGameTitle = function() {

// Background
var bitmap = ImageManager.loadTitle1('TitleBG');
var bg = new Sprite(bitmap);
this._bg = bg;
this.addChild(bg);

...

// Title Name
var bitmap = ImageManager.loadTitle1('TitleName');
var titleName = new Sprite(bitmap);
this._titleName = titleName;
this._bg.addChild(titleName);
}
I wanted to change it so that all my sprites are added in their own sub-methods, or in this case functions:

...


// Title Name
Scene_Title.prototype.drawName();
}

Scene_Title.prototype.drawName = function() {
var bitmap = ImageManager.loadTitle1('TitleName');
var titleName = new Sprite(bitmap);
this._titleName = titleName;
this._bg.addChild(titleName);
}
Appearently this._bg is undefined in the new function, so what am I doing wrong? Is it a simple Syntax error or have I missed something deeper? (I am most familiar with Ruby where this kind of thing should have worked)
 
Last edited:

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
5,599
Reaction score
6,552
First Language
Indonesian
Primarily Uses
RMVXA
You don't do this
Code:
Scene_Title.prototype.drawName();
You do this
Code:
this.drawName();
What you're calling is a prototype function.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,640
First Language
Czech
Primarily Uses
RMMV
problem is no tutorial I read said how to use a method/function, only how to define it.
Go through some c++ tutorials on object oriented programming. The basics should be enough.
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,729
Reaction score
4,685
First Language
English
Primarily Uses
RMMZ
Theo nailed it: use
Code:
this.drawName();
and not the prototype.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!

Forum statistics

Threads
106,035
Messages
1,018,459
Members
137,821
Latest member
Capterson
Top